0

I am using the below code to trigger the anchor click within the table compareTable, but it does not seem to take any effect. Can any1 point out the solution?

$('#compareTable a').click(function() {
alert("hi");
}); 

Here is the demo

1
  • 2
    This does not trigger, this binds an event handler... and in your demo, there are no anchors (<a>) in the table... Commented Jul 7, 2011 at 9:54

2 Answers 2

4

The <a> tag doesn't exist at the time you bind that click handler. You can solve this by using .delegate() or .live() (or binding the handler when you create the element). The former is usually considered preferable, but I find you markup difficult, so I'll share a quick workaround with .live(). Simple as can be:

$('#compareTable a').live('click', function() {
    alert("hi");
});
Sign up to request clarification or add additional context in comments.

Comments

1

jQuery's methods are two-folded. If you call them with empty arguments (that is, you don't pass any argument), then do what they mean. $('#something').click() means that it would be clicked. If you provide an argument which is usually a callback handler, they just register that handler. So, you should use:

 $('#copareTable a').click();

And of course, since you don't want to click those links without any reason, you probably should write this code in response to another event. Something like:

 $('#register').click(function(){
      $('#compareTable a').click();
 });

And also don't forget that $('#comparetTable a') is a collection of all anchor links inside that table. So if you send click directive, all of them would be clicked.

Comments

Your Answer

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