tags:

views:

48

answers:

4

Hello all,

I have a table with 5 columns,,when I click on first column on the row i need to get the all column row values using jquery?

Thanks

+1  A: 
$("td").click(function(){
  $(this).parent().find("td").each(function(){
    alert(this + " is one entry of your current row");
  });
});
marcgg
+2  A: 
$("td:first-child").click(function(){
  $(this).closest('tr').find("td").each(function(){
    alert(this.innerHTML);
  });
});
Reigel
+3  A: 

If you'd like your answer in an array:

$('table#mytable td:first-child').click( function(){
    var resultArray = $(this).closest('tr').find('td').map( function(){
        return $(this).text();
    });
    // Do something with resultArray
    // resultArray is a jQuery object
    // resultArray.get() is a plain array. get() can be chained above.
});
Ken Redler
Change the first instance of `});` to `}).get();` to return an array instead of a jQuery object.
Greg
Just a slight modification, I would add: $("#yourtable").find("td:first-child") instead of globally selecting any $('td:first-child').
Jeff Meatball Yang
@Jeff: Agreed. I was keeping it simple, but of course you're right that being more specific is better. I'll edit (and modify the selector rather than add the find()).
Ken Redler
@Greg: Yes, good point. I edited the answer (in comments clarifying the two options). In my experience the native array is almost always what you want.
Ken Redler
thanks for all your help my problem sovled..as per mark sujjested..
+1  A: 

Depends on what is actually IN the table a bit.

var mystuff = $("td").click().parent('tr').children('td').text();
var mystuff = $("td").click().parent('tr').children('td').innerHtml();

accessing them:

mystuff.each(function()
{
 //do stuff
};
mystuff.eq(2) // do stuff with second one
Mark Schultheiss