Ctrl_Alt_Defeat
Ctrl_Alt_Defeat

Reputation: 4009

Get value from jQuery Datarow

I have an asp.net page - I am using jQuery datatables on it.

Part of the code is below:

                        <% foreach (SubmissionSearchResult result in SearchResults)
                           {%>
                        <tr data-name='<%=result.ID %>'>  

So each time a row is drew on screen I am adding a data-name of result ID to it. What I want then is on clicking the row retrieve this ID.

I had something like the below to start :

        $('#searchResultsTbl').on("click", "tbody tr", function () {
            var nTds = $('td', this);

            var id = $(nTds[0]).text().trim();

This worked when the id was in the first td in the row but now the columns can be dynammic and it may not be in the first column. So I wanted to add the id to each row and then get it from the row so I added it as a data-name but not sure on how to get the value back from it?

Upvotes: 0

Views: 1900

Answers (3)

Sandeep Kumar
Sandeep Kumar

Reputation: 803

add the javascript function on click event of row

like

       <tr data-name='<%=result.ID %>' onclick='showid("<%=result.ID %>")'>  

Now in javascript define this showid function

        function showid(rowvalue)
         {
               alert(rowvalue);
          }

Upvotes: 0

sangram parmar
sangram parmar

Reputation: 8726

try this

    $('#searchResultsTbl tr:gt(0)').click(function () {
        var this_row = $(this);
        var data_name = this_row.attr('data-name');//row attribute
        var first_td = $.trim(this_row.find('td:eq(0)').html());
        var second_td = $.trim(this_row.find('td:eq(1)').html());
        var third_td = $.trim(this_row.find('td:eq(2)').html());//and so on
    });

Upvotes: 0

Lee Bailey
Lee Bailey

Reputation: 3624

            $('#searchResultsTbl').on("click", "tbody tr", function () {
                var id = $(this).data('name');
            });

Upvotes: 1

Related Questions