Saturday, December 29, 2012

Disable Enter Key event jquery java script

How to disable 'Enter Key' press key event using JQuery

As we know the default function of ENTER KEY is to submit HTML form values.

Sometimes we do not wish to submit the form values and want to do some other functionality. We can achieve this after disabling Enter Key press event and can call another event. The JQuery code sample is given here.

Example:
                        "Call any event of textbox when User press Enter Key"




$(document).ready(function() 
{
   $("input").bind("keydown", function(event) 


   {
     // track enter key
   var keycode = (event.keyCode ? event.keyCode : (event.which ? event.which :    event.charCode));

     if (keycode == 13)         //Here 13 is the keycode for enter key
     {
       // force the 'Enter Key' to implicitly call onchange event
       $(this).change();
        return false;
      }
      return true;
   });
});

Example:
                         "No action when User press Enter Key"

 
 $('input').keypress(function(event) 
    return event.keyCode == 13 ? false : true; 
});

0 comments:

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Powered by Code Imagine