Guy in the chair
Guy in the chair

Reputation: 1065

Pressing enter key doesn't search

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

Answers (4)

I wrestled a bear once.
I wrestled a bear once.

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

Makrand
Makrand

Reputation: 587

Have you tried live("keypress") ?

Upvotes: 1

Jahanzeb
Jahanzeb

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

Holybreath
Holybreath

Reputation: 413

$('.searchproductbrnad')   is it brand or brnad? typo?

Upvotes: 1

Related Questions