0

I have a HTML table like below.I'm going to access that values from jquery.But there's a problem

My HTML

                        <table class="table" id="examtbl">
                        <thead>
                            <tr>
                                <th>Ex No</th>
                                <th>Result</th>

                            </tr>
                        </thead>
                        <tbody>
                                <tr id="7500">
                                    <td id="examNo">
                                        <input class="form-control col-md-3 examNo" readonly="readonly" type="text" value="7500" />
                                    </td>
                                    <td>
                                        <input class="form-control col-md-3"  type="text" value="76" />
                                    </td>

                                </tr>
                                <tr id="7600">
                                    <td id="examNo">
                                        <input class="form-control col-md-3 examNo" readonly="readonly" type="text" value="7600" />
                                    </td>
                                    <td>
                                        <input class="form-control col-md-3"  type="text" value="66" />
                                    </td>

                                </tr>
                        </tbody>

                    </table>

My Script

 $("#examtbl > tbody > tr").each(function () {
       $(this).find('td').find("input").each(function () {
            alert($(".examNo").val()); < -- It hits3 times but everytime it shows me 7500 only.
       });  

   });

** Could you please give me a solution to how to get those values to.

3 Answers 3

3

the thing is, that you always call $(".examNo") ... and that means, he always looks for the first item that fits... you have to use this in the inner each loop :)

$("#examtbl > tbody > tr").each(function () {
       $(this).find('td').find("input").each(function () {
            alert($(this).val()); 
       });  
   });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table" id="examtbl">
    <thead>
        <tr>
            <th>Ex No</th>
            <th>Result</th>
        </tr>
    </thead>
    <tbody>
        <tr id="7500">
            <td id="examNo">
                <input class="form-control col-md-3 examNo" readonly="readonly" type="text" value="7500" />
            </td>
            <td>
                <input class="form-control col-md-3"  type="text" value="76" />
            </td>
        </tr>
        <tr id="7600">
            <td id="examNo">
                <input class="form-control col-md-3 examNo" readonly="readonly" type="text" value="7600" />
            </td>
            <td>
                <input class="form-control col-md-3"  type="text" value="66" />
            </td>
        </tr>
    </tbody>
</table>

Sign up to request clarification or add additional context in comments.

Comments

0
$("#examtbl > tbody > tr").each(function () {
   $(this).find('td').find("input").each(function () {
        alert($(this).val()); 
   });  
 });

Comments

0

You can use this

JS:

$("#examtbl > tbody > tr").each(function () {
                $(this).find('td > input').each(function () {
                    if ($(this).hasClass('examNo'))
                    alert($(this).val()); //< -- It hits3 times but everytime it shows me 7500 only.
                });  

            });

1 Comment

Check this fiddle

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.