views:

480

answers:

4

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>
A: 

This line needs the e:

$("#drivingSchoolInput").live("click", function(e) {
pygorex1
That still doesn't work.
Tom
Also, it needs to *not* be using the `click` event to test for keyboard events in an input field...
Shog9
Why does it need the `e`? Does Javascript check function arity nowadays?
ndim
@ndim: It needs `e` because he refers to `e` in the function.
Shog9
@Shog9: Oopsie. Need more coffee.
ndim
ha! can't believe I missed the click handler.
pygorex1
+2  A: 

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

o.k.w
The problem is that I don't know how to make it go to the next page.
Tom
Tried this and it still doesn't work$("#drivingSchoolInput").bind('keypress', function(e) { var code = e.keyCode || e.which; if(code == 13) { window.location = "http://www.google.com"; }
Tom
You want to pass the search text to google?
o.k.w
+1  A: 

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');
  }               
});
Shog9
document.location = $("#button1").attr('http://www.google.com'); this still didn't work. have you had a chance to test it?
Tom
Why are you passing "google.com" to the `attr()` function? There's no attribute with that name, and you want the value of the `href` attribute anyway...
Shog9
(and yes, i tested it)
Shog9
+2  A: 

Write a small jQuery plugin:

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

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

I hope this helps.

Jacob Relkin