Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery loop each first td in every row

Tags:

jquery

each

I have the following jquery that adds a onclick to every image within a table

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

This works ok, however I would like to modify it so that only an image in the first TD of every row in the table gets the onclick event.

I have tried the following, but it does not work

$("#listtable tr td:first img").each(
like image 815
Rippo Avatar asked Dec 29 '22 03:12

Rippo


2 Answers

Try this, using the :first-child selector

$("#listtable tr td:first-child img").click(function() {
  $(this).load("/Admin/ChangeIdeaStatus/" + $(this).attr('rel'));
});
like image 117
Nick Craver Avatar answered Mar 12 '23 11:03

Nick Craver


Alternatively you could set a .class or #id attribute to the first image of every row and then just select that element to have an onclick event. This might be faster than for-looping if you have a large set.

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

like image 23
Brian Liang Avatar answered Mar 12 '23 11:03

Brian Liang