Click here to Skip to main content
15,883,844 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi -

I have a dynamically created table that loads more elements on button click. What I'm also trying to do is when any where in that table when a row is clicked no matter what row get the value in the first cell of that row clicked. I keep get the first value of the first row first cell with my code.

Here is the javascript:

C#
$(".browseTable").click(function () {
    var ptsID = $(this).find('td:first').text();

    //var ptsID = $('td:first').text();
    console.log(ptsID);

});


Here is the table
HTML
<table id="displayMe" class="browseTable">
    <tr>
        <th>
            ID
        </th>
        <th>
            Image
        </th>
        <th>
            Company
        </th>
    </tr>
    <tbody>
    </tbody>
</table>
Posted

1 solution

The problem is, you're asking jQuery to find the first td in the table. You need to ask it for the first td in the row.

Something like this should work:
JavaScript
$(".browseTable").on("click", "td", function(){
    var ptsID = $(this).parent("tr").find("td:first").text();
    alert(ptsID);
});

https://jsfiddle.net/9pbmb1tj/[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900