Stop reloading page with 'Enter Key'
Don't bind to the input
s; bind to the form
. Assuming the form
has an ID of searchForm
:
$("#searchForm").submit(function() {
search($("#searchText").get(0));
return false;
});
Try it out.
It can also be done with plain JavaScript:
document.getElementById('searchForm').addEventListener('submit', function(e) {
search(document.getElementById('searchText'));
e.preventDefault();
}, false);
I know its a little late but I ran into the same problem as you. It worked for me using "keypress" instead of bind.
$('#searchText').keypress(function (e) {
if (e.which == 13) {
e.preventDefault();
//do something
}
});
You are missing #
in the selector. Try this
<input type='text' id='searchText' />
JS
$("#searchText").bind('keyup', function(event){
if(event.keyCode == 13){
event.preventDefault();
//$("#buttonSrch").click();
search(this.value);
}
});
Add onSubmit="return false;"
on your form tag
<form onSubmit="return false;">
/* form elements here */
</form>`
$('#seachForm').submit(function(e){
e.preventDefault();
//do something
});