Reputation: 1065
Searching event isn't triggered with enter key but works fine if manually press Search button. Here's my simple script
$('#searchproductbrand').live('click',function() {
var search = $('.searchproductbrand').val();
window.open('search.php?search='+search,'_self');
});
$('.searchproductbrand').keypress(function(e) {
if (e.which == 13) {
var search = $('.searchproductbrand').val();
window.open('search.php?search='+search,'_self');
}
});
And here is my textbox & button.
<input type="search" id="text" class="searchproductbrand" placeholder="Search for Product, Brand" onkeyup="showResult(this.value)" autofocus="autofocus" />
<input type="button" id="searchproductbrand" class="button" value="Search" style="padding: 10px 10px;"/>
So pressing button works fine but pressing enter key while searching doesn't work. Anyone can help?
Upvotes: 0
Views: 1385
Reputation: 23409
pressing enter submits a form, so just make it a proper form...
<script>
function myFunction(){
//do stuff
return false;
}
</script>
<form onsubmit="return myFunction();">
<input type="search" id="text" class="searchproductbrand" placeholder="Search for Product, Brand" onkeyup="showResult(this.value)" autofocus="autofocus" />
<input type="submit" id="searchproductbrand" class="button" value="Search" style="padding: 10px 10px;"/>
</form>
Upvotes: 1
Reputation: 613
You have to create the document
object.
Change your keypress
code like this
$(document).keypress(function(e) {
if (e.which == 13) {
var search = $('.searchproductbrand').val();
window.open('search.php?search='+search,'_self');
}
});
I hope, it will solve your issue.
Upvotes: 0