Wednesday, January 16, 2013

onkeypress and onkeydown, prevent enter key to submit a form in input

In JavaScript, the keypress event fires after the key is pressed and released, while keydown fires after the press but before the release.
     <script type="text/javascript">
      function handleKeyPress(e,form){
         var key=e.keyCode || e.which;
           if (key==13){
     <button  type='button' id="filter"  onclick="searchstudent()"> Serach </button>
    <input type="text"     id="search"  maxlength="80" onkeypress="handleKeyPress(event,this.form)" onkeyup="searchstudent()" >

In this example, when any key is pressed, Javascript function searchstudent() is called. We do not have to wait all the text  typed to start search. Somehow it is similar to Ajax.

 To prevent enter key or return key (key code 13) submit the form, we defined a user function handleKeyPress in onkeypress, which called e.preventDefault();   to prevent form submission.

1 comment:

  1. Git and Github Online Training
    What is Git?
    Installing Git
    Getting Started
    Git Concepts and Architecture
    Making Changes to Files
    Undoing Changes
    Ignoring Files
    Navigating the Commit Tree
    Merging Branches
    Stashing Changes
    Tools and Next Steps
    Real-time Scenarios

    Git and Github Online Training, ONLINE TRAINING – IT SUPPORT – CORPORATE TRAINING The 21st Century Software Solutions of India offers one of the Largest conglomerations of Software Training, If you’re serious about a career in IT, 21st Century would like to provide you a guidance don’t hesitate to organize a free demo session. For any further information regarding the courses once go through our website Visit: | Call Us +917386622889 - +919000444287 -