Jquery loop every first td in every line

I have the following jquery that adds onclick to each image in a table

$("#listtable tr td img").each( function(intIndex) { $(this).click(function() { $(this).load("/Admin/ChangeIdeaStatus/" + $(this).attr('rel')); }); } ); 

This works fine, however I would like to change it so that only the image in the first TD of each row of the table receives the onclick event.

I tried the following but it does not work

 $("#listtable tr td:first img").each( 
+4
source share
2 answers

Try this using : first-child selector

 $("#listtable tr td:first-child img").click(function() { $(this).load("/Admin/ChangeIdeaStatus/" + $(this).attr('rel')); }); 
+8
source

Alternatively, you can set the .class or #id attribute for the first image of each line, and then simply select this element to have an onclick event. This can be faster than for-looping if you have a large set.

so $(".firstImageOfTr").each( ...

+2
source

Source: https://habr.com/ru/post/1299825/


All Articles