I have a search box at the top of page that makes an ajax call when a user hits the adjacent button. I am trying to update the input tag so that when a user hit the 'enter' key, the apropriate JavaScript takes place without reloading the page. The problem is that the page keeps reloading. Here is my latest attempt:
$("searchText").bind('keyup', function(event){
if(event.keyCode == 13){
event.preventDefault();
$("#buttonSrch").click();
return false;
}
});
<input type='search' id='searchText' />
<input type='button' id='buttonSrch' onclick="search(document.getElementById('searchText'))" value='Search' />
This is what ended up working for me. Please note that my struggle was to find the object that triggered the form submit:
});
You could set the form action attribute to javascript:void(0); that way the form doesn't post/get so the page wont refresh.
I know its a little late but I ran into the same problem as you. It worked for me using "keypress" instead of bind.
You are missing
#
in the selector. Try thisJS
Don't bind to the
input
s; bind to theform
. Assuming theform
has an ID ofsearchForm
:Try it out.
It can also be done with plain JavaScript: