Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clicking a link when enter key is pressed using jQuery

I'm trying to make it so when a user is in a text box and they press enter, it is the same as clicking the link, in which case it should take them to another page. Here's what I have and it doesn't work.

The code

//jQuery 
$(document).ready(function() {

  //if focus is in the input box drivingSchoolInput
  $("#drivingSchoolInput").live("click", function() {

    //if enter key is pressed
    if(e.keyCode == 13) {

        //click the button and go to next page
        $("#button1").click();
    }       
  });   
});

The markup

<form>      
  <div class="formDiv">
    <label for="City">Search by Driving School</label>
    <span class="inputBox"><input type="text" name="City" class="input" id="drivingSchoolInput" /></span>
  </div>

  <h4 class="submitButton"><a href="school.html" id="button1">Submit</a></h4>     
</form>
like image 373
Tom Avatar asked Nov 30 '09 02:11

Tom


People also ask

How do you trigger click event on pressing enter key?

To trigger a click button on ENTER key, We can use any of the keyup(), keydown() and keypress() events of jQuery. keyup(): This event occurs when a keyboard key is released. The method either triggers the keyup event, or to run a function when a keyup event occurs.

When enter key is pressed jQuery?

The “enter” key is represent by code “13”, check this ASCII charts. To check if an “enter” key is pressed inside a textbox, just bind the keypress() to the textbox. $('#textbox').

How do you detect when a keyboard key has been pressed using jQuery?

The keypress() method in jQuery triggers the keypress event whenever browser registers a keyboard input. So, Using keypress() method it can be detected if any key is pressed or not.

How do you check enter key is pressed in JavaScript?

Check the event.keyCode property. The keyCode property returns a number for the key that's pressed instead of a string with the key name. When it returns 13, then we know the enter key is pressed. });


4 Answers

Write a small jQuery plugin:

jQuery.fn.enter = function(callback) {
   if(!callback) {
      //don't attach if we have garbage.
      return;
   }

   $(this).keydown(function(e) {
       var ev = e || event;
       if(ev.keyCode == 13) {
          callback();
          return false;
       }
   }); 
};

Usage: $(element).enter(callback_func);

I hope this helps.

like image 76
Jacob Relkin Avatar answered Oct 13 '22 00:10

Jacob Relkin


Check this out: jQuery Event Keypress: Which key was pressed?

I'll just consolidate the codes from that post here:

$('#searchbox input').bind('keypress', function(e) {
 var code = e.keyCode || e.which;
 if(code == 13) { //Enter keycode
   //Do your stuff + form submit
 }
});

PS: I have never tested it, but it 'should' work. :P

like image 20
o.k.w Avatar answered Oct 13 '22 00:10

o.k.w


You have a few major problems with this code...

The first one, pygorex1 caught: you need to specify the event argument if you wish to refer to it...

The second one is in the same area of your code: you're trying to check for the key event in a handler for the click event!

The third one can be found on this line:

            //click the button and go to next page
            $("#button1").click();

...which does nothing, since you have no event handlers on that link, and jQuery's click() function does not trigger the browser's default behavior!

Instead, try something like this:

// if a key is pressed and then released
$("#drivingSchoolInput").live("keyup", function(e) {

  // ...and it was the enter key...
  if(e.keyCode == 13) {

    // ...navigate to the associated URL.
    document.location = $("#button1").attr('href');
  }               
});
like image 44
Shog9 Avatar answered Oct 12 '22 22:10

Shog9


I wanted to do something similar, so after reading David K Egghead's suggestion above I came up with this which triggers a "click" even on an anchor when enter is pressed. Could be used to "click" a button as well!

$(document).keypress(function(event){if(event.keyCode==13){$('.save_icon').trigger("click");}});
like image 27
Alan Avatar answered Oct 12 '22 22:10

Alan