Can I call jQuery's click() to follow an <a> link if I haven't bound an event handler to it with bind or click already? Can I call jQuery's click() to follow an <a> link if I haven't bound an event handler to it with bind or click already? jquery jquery

Can I call jQuery's click() to follow an <a> link if I haven't bound an event handler to it with bind or click already?


Another option is of course to just use vanilla JavaScript:

document.getElementById("a_link").click()


Interesting, this is probably a "feature request" (ie bug) for jQuery. The jQuery click event only triggers the click action (called onClick event on the DOM) on the element if you bind a jQuery event to the element. You should go to jQuery mailing lists ( http://forum.jquery.com/ ) and report this. This might be the wanted behavior, but I don't think so.

EDIT:

I did some testing and what you said is wrong, even if you bind a function to an 'a' tag it still doesn't take you to the website specified by the href attribute. Try the following code:

<html><head><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script> <script>  $(document).ready(function() {   /* Try to dis-comment this:   $('#a').click(function () {    alert('jQuery.click()');    return true;   });   */  });  function button_onClick() {   $('#a').click();  }  function a_onClick() {   alert('a_onClick');  } </script></head><body> <input type="button" onclick="button_onClick()"> <br> <a id='a' href='http://www.google.com' onClick="a_onClick()"> aaa </a></body></html> 

It never goes to google.com unless you directly click on the link (with or without the commented code). Also notice that even if you bind the click event to the link it still doesn't go purple once you click the button. It only goes purple if you click the link directly.

I did some research and it seems that the .click is not suppose to work with 'a' tags because the browser does not suport "fake clicking" with javascript. I mean, you can't "click" an element with javascript. With 'a' tags you can trigger its onClick event but the link won't change colors (to the visited link color, the default is purple in most browsers). So it wouldn't make sense to make the $().click event work with 'a' tags since the act of going to the href attribute is not a part of the onClick event, but hardcoded in the browser.


If you look at the code for the $.click function, I'll bet there is a conditional statement that checks to see if the element has listeners registered for theclick event before it proceeds. Why not just get the href attribute from the link and manually change the page location?

 window.location.href = $('a').attr('href');

Here is why it doesn't click through. From the trigger function, jQuery source for version 1.3.2:

 // Handle triggering native .onfoo handlers (and on links since we don't call .click() for links) if ( (!elem[type] || (jQuery.nodeName(elem, 'a') && type == "click")) && elem["on"+type] && elem["on"+type].apply( elem, data ) === false )     event.result = false; // Trigger the native events (except for clicks on links) if ( !bubbling && elem[type] && !event.isDefaultPrevented() && !(jQuery.nodeName(elem, 'a') && type == "click") ) {     this.triggered = true;     try {         elem[ type ]();         // Prevent Internet Explorer from throwing an error for some hidden elements     }     catch (e)     {     } }

After it calls handlers (if there are any), jQuery triggers an event on the object. However it only calls native handlers for click events if the element is not a link. I guess this was done purposefully for some reason. This should be true though whether an event handler is defined or not, so I'm not sure why in your case attaching an event handler caused the native onClick handler to be called. You'll have to do what I did and step through the execution to see where it is being called.