0

So I have this code on a page:

<a id="buy_now" class="btn_green_release" href="#"><span>BUY NOW!</span></a>

It's for a link button, not a standard input button.

What I want to do, is to have it triggered when I press a key. I've found something around here (link below) but it didn't work.

Trigger a button click with JavaScript on the Enter key in a text box

Any help is highly appreciated!

Andrei

Community
  • 1
  • 1
Andrei P.
  • 11
  • 1

2 Answers2

0

Try this : http://jsfiddle.net/2UCp7/jsfiddle

$("#buy_now").click(function(){
    alert('clicked');
    return false;
});

$('body').keyup(function(event){
    $("#buy_now").trigger('click');
    console.log('press any key');
    if(event.keyCode == 13){
        console.log('press enter');
        $("#buy_now").trigger('click');
    }
});

NOTE 1: key 13 is for ENTER

NOTE 2 : I delegate the click in a function

NOTE 3 : better to use preventDefault instead of return false !

rebe100x
  • 1,473
  • 15
  • 18
0

Figured it out and it's actually simple:

$('body').keyup(function(event){
if(event.keyCode == 13){
     document.getElementById('buy_now').click();
}});

It works. :)

'ndrei

Andrei P.
  • 11
  • 1