views:

29

answers:

1

Hi. I added codes (jQuery) for keyPress and click of search textbox and button respectively. Now the search query reflects in the browser (http://samplesite/default.aspx?k=query) when I click the button, but it does not when I press enter inside the search textbox. Anyone know this issue? Thanks.

// Keypress event for search box 
$('#search-box').keypress(function (e) {
    if ((e.which && e.which === 13) || (e.keyCode && e.keyCode === 13)) {
        Search();
    };
});
// Click event for search button 
$('#search-button').bind('click', function () {
    Search();
});

p.s. im using location.assign

A: 

You can try this:

$('#search-button').bind('click', function () {
    Search();
}); // order is important!

$('#search-box').bind(($.browser.opera ? "keypress" : "keydown"), function (e) { // opera handle keypress differently
  if (e.keyCode && e.keyCode === 13) {
    $('#search-button').click();
  };
});

Also, you can try jquery.hotkey too.

Ionut Staicu
Thanks. I will try this and will let you know. ^_^
SajmiraZ
k parameter is still not updating, I am using IE 7. Is there an issue with location.assign? or its just that keyPress wont update the url in the browser textbox in IE 7 and only when you click a button? Thanks again.
SajmiraZ
i never used `location.assign`. But you can try to use `window.location.assign` instead. Try to debug with `console.log()` or just `alert()` to see where the whole thing is stuck.
Ionut Staicu
I will try your suggestion. I tried debugging this and found out that the variable that holds the URL is the same on both click and keypress (before passing it to location.assign) but its just that keypress wont update the url in the browser. Thanks again.
SajmiraZ
But you know you can use `$('form').submit()`, right? (or you use that dumb thing called web forms? )
Ionut Staicu
haha! already did that but to no avail. Wonder how the browser handles the enter key. hmmm.. need to investigate on this further. Hope someone can help. Thanks a lot!
SajmiraZ
However, i think you should investigate the `Search()` function, because the code i gave you should work on all browsers.
Ionut Staicu
I got it. I just added e.preventDefault before calling Search/click and it worked. I think the browser evaluates my 'Enter' as submit.
SajmiraZ