2

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

3 Answers 3

6

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
});
Sign up to request clarification or add additional context in comments.

3 Comments

He wanted the link to be on the anchor, so I suggest it should rather be $('td.rightImage a').click(function() { // Your code here });
I tried both of these but I'm getting an error in my console :/
this info about dynamic load was remarkable
2

Please try this code

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.