Call function if enter key is pressed

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • apurvaG
    New Member
    • Apr 2007
    • 14

    Call function if enter key is pressed

    Hi,

    I have a text control in my form. When I press a Enter key in this control , it should called another javascript function, but it is not doing so instead it is triggering "Submit" button event. Can you please help.

    I have wrote a onkeypress function as follows:

    <input type="text" id="txt1" style="width:30 0px" MaxLength="50" onkeypress="jav ascript:testEnt er()"/>

    function testEnter() {
    if(event.keyCod e==13){
    anotherjavascri pt();
    }
    }


    Thanks in advacne
    Apurva G
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Either swallow the enter by setting it to null
    Code:
    if(event.keyCode==13){
    		event.keyCode = null;
    		//call function
    	}
    or better set it to tab which I think will be
    Code:
    event.keyCode = 9;

    Comment

    Working...