user354625
user354625

Reputation: 557

How to get the Particular column value using jquery

I used this code to get the particular column value using jquery..

$("#Grid td:first-child").click(function() {
                var resultArray = $(this).closest('tr').find('td').text();
                   alert(resultArray);
            });

How do I get the particular column value? That is, I need grid 4th and 5th column value?

Upvotes: 2

Views: 12316

Answers (2)

Reigel Gallarde
Reigel Gallarde

Reputation: 65264

$('#Grid td:first-child').click( function(){
    var resultArray = $(this).closest('tr').find('td').map( function(){
        return $(this).text();
    }).get();

    alert(resultArray[2]); // third
    alert(resultArray[3]); // fourth..

});

or

$('#Grid td:first-child').click( function(){
    var resultArray = $(this).closest('tr')
        //     third     fourth 
        .find('td:eq(2), td:eq(3)').map( function(){
             return $(this).text();
        }).get();

    alert(resultArray[0]); 
    alert(resultArray[1]); 

});

Upvotes: 1

Sarfraz
Sarfraz

Reputation: 382696

Use the :eq selector:

$("#Grid td:first-child").click(function() {
   var value = $(this).closest('tr').find('td:eq(2)').text(); // for third column
   alert(value);

   var value = $(this).closest('tr').find('td:eq(3)').text(); // for fourth column
   alert(value);
});

That will alert value of 3rd and 4rth TD/column when first td of element with id Grid (td:first-child) is clicked.

If however, you want an array of values of TDs, use the map and get methods like this:

$("#Grid td:first-child").click(function() {
   var value_array = $(this).closest('tr').find('td').map(function(){
     return $(this).text();
   }).get();
});

Now value_array will contains text for found TDs eg:

value_array[0] // first
value_array[1] // second
value_array[2] // third

Upvotes: 4

Related Questions