How to detect ENTER key press in HTML textbox?

Suppose you have a search form in your web page. User type search keyword in the textbox(input tag) and press the ENTER key instead of click on the search button. How to detect ENTER key press HTML input tag?

<input type="text" id="SearchKeyword"/>
<button type="button" onclick="Search()">Search</button>
//Using JQuery
    if(e.keyCode == 13)

//Using JavaScript
document.getElementById("SearchKeyword").onkeypress = function(e)
    if (e.keyCode === 13)
        alert("Enter key pressed");
Final code and demo is here


No comments:

Post a Comment