jQuery blur and the enter key

Jon Harris picture Jon Harris · Mar 25, 2011 · Viewed 27.7k times · Source

I am trying to update a div dynmically with a googlemap based on a zip code being entered. Its working fine if the user tabs away from the input box (id ZipCode) but if they press enter it doesn't blur the field. I have got a variation on this working with onkeyup, but one event calls the other and its a mess. Is it possible to kick the function off if either event occurs.

$(document).ready(function() {
    $("#ZipCode").blur(function() {
        $("#dynamic").load('googlemap.php', {zipcode: $('#ZipCode').val(), maxdistance:  $("input[name='MaxDistance']:checked").val() }, 
            function() {
            });
        });
}) ;

Thanks for any pointers.

Answer

Town picture Town · Mar 25, 2011

You can do this:

 $(document).ready(function() {    
     $("#ZipCode").bind('blur keyup',function(e) {  
          if (e.type === 'blur' || e.keyCode === 13)  
          // do your stuff here  
     });  
  });

Syntax: event.keyCode
Return Value: A Number, representing either a Unicode character code or the Unicode key code