user3789344
user3789344

Reputation: 81

javascript function after pressing enter key on text box

I have a text input

<input type="text" name="invCode_' + count + '" style="height:15px;width:55px;" onchange="loadPIEname(this,' + count + ' );"/>

and a javascript function for doing some function

that works fine.But How to make the function call when i press enter key on that text box? thanks in advance.

Upvotes: 1

Views: 6340

Answers (3)

Alok Agarwal
Alok Agarwal

Reputation: 3099

You can use jQuery to do same

$("#id_of_textbox").keyup(function(event){
    if(event.keyCode == 13){
        $("#id_of_button").click();
    }
});

this will work for you.

using plane jave script if u want please use:

<input type="text" id="txtSearch" onkeydown="if (event.keyCode == 13) document.getElementById('btnSearch').click()"/>

Upvotes: 3

Jordi Puig
Jordi Puig

Reputation: 224

<input onkeypress="return myscript(event,this)" ...

Then do

function myscript(e,i){
    if(e.keycode == 13){
        loadPIEname(i,' + count + ');
    }
}

Upvotes: 3

Ahmad
Ahmad

Reputation: 12737

Just use onkeypress instead of onchange event and check for which key was pressed inside another function like this:

===HTML===

<input type="text" onkeypress="searchKeyPress(event,this,' + count + ' );"/>

===Javascript===

 function searchKeyPress(e,args)
    {
        // look for window.event in case event isn't passed in
        if (typeof e == 'undefined' && window.event) { e = window.event; }
        if (e.keyCode == 13)
        {
            loadPIEname(args);   //call your function here
        }
    }

Upvotes: 2

Related Questions