0

may peace on you !

Guys i am stuck in a query i want to get the value of row when a image in that row is clicked.so the purpose of update by getting that row id.i got the code below but it get me the row# and col# not the id.below is code can someone help me.

$('td').click(function(){
  var col = $(this).parent().children().index($(this));
  var row = $(this).parent().parent().children().index($(this).parent());
  alert('Row: ' + row + ', Column: ' + col);
});
3
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by jQueryLover
0

jQl,

For each image you want to have this behaviour, give it a class (below I use someclass ), then attach the behaviour as follows:

$('img.someclass').click(function(){
  var $row = $(this).closest('tr');
  alert('Row id: ' + $row.attr('id'));
});

.closest() is safer than .parent() and increasingly safer than .parent().parent() etc. as there may (one day) be intermediate nodes that you didn't consider at the time of writing the js. This is unlikely in the case of <td> to <tr> but in the case of <td> to <table> there is uncertainty as to whether there is an intermediate <tbody>.

In jQuery, .closest() can be one of your best friends.

Airshow

Edited by Airshow: n/a

0

what is this part for?

var col = $(this).parent().children().index($(this));

looks like you are trying to get the same td again which ultimately is $(this).

$('td').click(function(){
    //$(this) would be the clicked td.
});
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.