user3120015
user3120015

Reputation: 181

Adding a Jquery onclick event to all links within a certain class

How do I add a Jquery onclick event that looks for any link like the below so it needs to be in a td and have a class of "rightImage"

  <td class="rightImage"><a href="somelink.aspx"><img src="someimage.jpg"alt=""></a></td>

The click event needs to be:

    _gaq.push(['_trackEvent', 'sidebanner', 'click', $(this).attr('href'),o,true]);

Upvotes: 2

Views: 16143

Answers (3)

Felix
Felix

Reputation: 38102

You can use . to target class in jQuery and use .click() to handle click event:

$('td.rightImage a').click(function() {
     // Your code here
});

If your td have been added dynamically (added after page load), you can use event delegation:

$("body").on("click", "td.rightImage a", function() {
    // Your code here
});

Upvotes: 6

Gwenc37
Gwenc37

Reputation: 2048

It should be :

$('td.rightImage a').click(function(){
    ...
});

(like in css for selectors)

but your tag needs to be in a table, otherwise it will not work

Upvotes: 0

Aju Mon
Aju Mon

Reputation: 235

Please try this code

$(".rightImage").click(function(){
  //do something necessary here
});

Upvotes: 2

Related Questions