Search code examples
javascriptjquerydom-traversal

Take specific item of the table?


I am with the following function:

$ ("# GridView1 tr"). Click (function (e){

                 var $ cell = $ (e.target). closest ("td");
                 $ ("# TextBox3"). Val ($ cell.text ())
   }

What makes the TextBox3 receive the value of clicked square on the table, what I needed was to get a specific value of the same row of the table by clicking on any square in that row. You can do this?


Solution

  • Since your jQuery event targets the tr element, code inside the event callback will have this assigned to that element's DOM object by default. You can use that to find any of its children (any of the cells in the row).

    $('#GridView1 tr').click(function() {
        /**
         * You can grab the `td` you want by specifying its index or using a
         * class selector.
         */
        var desired_cell_index = 5;
        var desired_cell = $(this).find('td').eq(desired_cell_index);
    
        $('#TextBox3').val(cell_data.text());
    });