1

I am trying to get the value of my td column with a specific class (tablecellname):

                    $('#MainContent_myTable tr').each(function () {
                        console.log($('.tablecellname').html());
                    });

I think I get the correct amount of rows in the table but it only gives me the 1st value (repeated).

<table id='MainContent_myTable'>
    <tr>
        <td class='tablecellname'>abc</td>
        <td class='tablecellcode'>111</td>
    </tr>
    <tr>
        <td class='tablecellname'>def</td>
        <td class='tablecellcode'>222</td>
    </tr>
    <tr>
        <td class='tablecellname'>ghi</td>
        <td class='tablecellcode'>333</td>
    </tr>
    <tr>
        <td class='tablecellname'>jkl</td>
        <td class='tablecellcode'>444</td>
    </tr>
    <tr>
        <td class='tablecellname'>mno</td>
        <td class='tablecellcode'>555</td>
    </tr>
    <tr>
        <td class='tablecellname'>pqr</td>
        <td class='tablecellcode'>666</td>
    </tr>
    <tr>
        <td class='tablecellname'>stu</td>
        <td class='tablecellcode'>777</td>
    </tr>
    <tr>
        <td class='tablecellname'>vwx</td>
        <td class='tablecellcode'>888</td>
    </tr>
    <tr>
        <td class='tablecellname'>yz</td>
        <td class='tablecellcode'>999</td>
    </tr>
</table>

It only prints abc over and over again.

What am I doing incorrectly?

2 Answers 2

4

$('.tablecellname') selects all elements in the entire document that match. Instead, in your function you want to find children of the currently iterated element:

$(this).find('.tablecellname').html()
1
  • 1
    Or you can use a contextual selector (my preference) $('.tablecellname', this).html(); Sep 5, 2013 at 15:32
1

$('#MainContent_myTable tr').each does count each occurrence of <TR> however using $('.tablecellname').html()) will only return the first instance of the class to occur in the html.

You'll need to use $(".tablecellname").each

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.