Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enter key triggering link

I have a JQuery scroller on a page where each item is a div with an id. each div has a link to the next div in the scroller (all on the same page)

$('a.panel').click(function () {
};

I have a click event to all links with the 'panel' class where I check which links was clicked and then do some ajax processing accordingly:

 if($(this).attr('href')=="#item2")
{
//do some processsing 
}

and once the processing is done I use the scrollTo JQuery method to scroll to the next div

I need to have it that the user can press the enter key instead of clicking on the link. Now the problem is: a. I have several links on the same page that all need to have this behaviour. b. I need to differentiate which link triggered the click event and do some server-side processing.

Is this possible at all?

I appreciate the quick and helpful responses!!Thanks a million for the help!

like image 436
NinaNa Avatar asked Aug 04 '11 19:08

NinaNa


1 Answers

Focus + enter will trigger the click event, but only if the anchor has an href attribute (at least in some browsers, like latest Firefox). Works:

$('<a />').attr('href', '#anythingWillDo').on('click', function () {

    alert('Has href so can be triggered via keyboard.');

    // suppress hash update if desired
    return false;

}).text('Works').appendTo('body');

Doesn't work (browser probably thinks there's no action to take):

$('<a />').on('click', function () {
    alert('No href so can\'t be triggered via keyboard.');
}).text('Doesn\'t work').appendTo('body');
like image 152
Brandon Hill Avatar answered Oct 06 '22 18:10

Brandon Hill