Is there a Javascript or jQuery event that fires after a targeted link with the keyboard?

I use jQuery focus() and blur() to highlight links to the image while navigating the keyboard (tabbing), but should be able to run some code when a link is followed by 'Enter' on the keyboard.

Is there an inline event that does this, or do I need to do something like bind keypress and check the 'Enter' key?

+4
source share
3 answers
 <a href="http://www.google.com" id="testlink">CatchMe</a> $('#testlink').attr("rel", $('#testlink').attr("href")); $('#testlink').attr("href",""); $('#testlink').keydown(function(event) { if (event.keyCode == '13') { alert("Don't you dare!"); } if (event.keyCode == '71') { location.href=$('#testlink').attr("rel"); } }); 

This code removes href (saves it to rel). Now you can catch keydowns. You can answer the enter key (13) as you like (in this case a warning). Subsequently, you can let the browser follow the link if you want. However, in this example, I allow the browser to follow the link when the g key is pressed (code 71).

Note that this also works when the href value is similar to "javascript: alert (" blah ").

Edit: this is much simpler (inspired by the answer to this question):

 $('#testlink').click(function () {alert("hi"); return false; }); 

(return true if you want the link to follow the warning)

+2
source

Take a look at the following:

focusin

focusout

Also, if you enter the key processing scene, please read quirksmode

The final link is very important because each browser is different in its own way, i.e. some events are ignored, some of them are fired when you do not expect them.

0
source

Work out the answer of Yochem. You can use .trigger () to create your own events as follows:

 $('#testlink').keydown(function(event) { if (event.keyCode == '13') { // alert("Don't you dare!"); $('#testlink').trigger('enterdown'); } }); 

and:

 $('#testlink').bind('enterdown', function(event) { alert('Enter was pressed.'); }); 
0
source

All Articles