Tuesday, September 18, 2012

How to disable Enter Key event JQuery java script

Disabling Enter Key press default form submission event using Jquery/Java Script

As we know that when we press Enter key the form automaticaly submited to the server. Some times we need to handle this situation and want to stop this automatic form submission. For this functionality we can disable Enter key 'KeyPress' event.

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

Description: Here we are working with keypress event over any input type and checking keyCode, if it is 13, means the user pressed 'Enter Key'. We can also use this keypress method inside the $(document).ready() function.

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

0 comments:

 
Powered by Code Imagine