I have a form which performs a javascript when submit is clicked. I can't seem to figure out how to do the same thing when the return key (13) is used.
HTML: (note MakeRequest() is a JS method which performs a request on another php page and returns results to JSresult.)
<form name="SearchForm">
Name: <input type = "text" name = "Name" id="Search"
placeholder="Search stuff here...">
<button type="button" id "Request" onClick="MakeRequest()"">Search</button>
</form>
div id="JSresult">
</div>
Replace your button with a submit input and move the function to form onSubmit, instead of button onClick :
<form name="SearchForm" onSubmit="MakeRequest();">
Name: <input type = "text" name = "Name" id="Search" placeholder="Search stuff here..." />
<input type="submit" id="Request" value="Search" />
</form>
Here is an unobtrusive approach that should do the trick:
<script>
window.onload = function(){
document.getElementById("Search").onkeydown = function(e){
key = (e.keyCode) ? e.keyCode : e.which;
if(key == 13) {
document.getElementById("Request").click();
}
};
};
</script>
Just copy that into the head
of your HTML file.