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?
Eg:
Demo
Eg:
<input type="text" id="SearchKeyword"/>
<button type="button" onclick="Search()">Search</button>
<script>Final code and demo is here
//Using JQuery
$('#SearchKeyword').keyup(function(e)
{
if(e.keyCode == 13)
{
$(this).trigger("enterKey");
}
});
//Using JavaScript
document.getElementById("SearchKeyword").onkeypress = function(e)
{
if (e.keyCode === 13)
{
alert("Enter key pressed");
Search();
}
};
</script>
Demo
Comments
Post a Comment
Ask me anything here...