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);
});

Recommended Answers

All 3 Replies

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

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.
});
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.