// catch enter code in search form in front page
$('#search').keypress(function (e) {
var str = $('#search').val();
var url = "default.aspx?search=" + str;
if (e.keyCode == 13) {
location.href = url;
}
});
I don't know why this code doesn't work what I expected " When you enter something in input#search, check if it's not empty then redirect to another page ". I try to enter every line in console without checking event, it works!
How can I fix this and why it doesn't work ? Thanks for your consideration time :)
// catch enter code in search form in front page
$('#search').keypress(function (e) {
var str = $('#search').val();
var url = "default.aspx?search=" + str;
if (e.keyCode == 13) {
location.href = url;
}
});
I don't know why this code doesn't work what I expected " When you enter something in input#search, check if it's not empty then redirect to another page ". I try to enter every line in console without checking event, it works!
How can I fix this and why it doesn't work ? Thanks for your consideration time :)
You might try .keyup() instead of .keypress(). Keypress is not an official specification, and can have unfortunate consequences in some browsers.
Put your domain including http for location href to work correctly
// catch enter code in search form in front page
$('#search').keypress(function (e) {
var str = $('#search').val();
var domain = "http://www.yourdomain.";
var url = domain+"default.aspx?search=" + str;
if (e.keyCode == 13) {
location.href = url;
}
});