views:

265

answers:

3

Hi Guys

Just wondering if anyone knows of a way that wiring up jquery to run a function for when a user clicks on a link or tabs to a link and hits enter.

I want to intercept that activation of a link and perform an action before the page is changed, but I want to do it in either case.

Cheers Anthony

+1  A: 
Zaagmans
+4  A: 

The 'click' event will fire in both cases, this will get you what you want:

$('a').click(function(){
    alert('perform action here');
});
pjesi
Is it possible to get the URL of the link that has been clicked?
Dmitri Nesteruk
sure: alert($(this).attr('href'));
pjesi
+1  A: 

It's pretty simple actually... When pressing enter on an element, it acts as a click in browsers. No need to do anything special.

$('a.links_to_bind').bind('click', function() { 
    /* do your stuff... */
    return false;
});

Edit: If you want the page to change after your actions are complete, you may want to add a conditional statemenet to that return false. Something like:

if(everythings_good) {
    return true;
} else {
    return false;
}
KyleFarris