I created a search field (id:searchField) and a search button (id:searchButton) using Xpages Custom Controls. I added an onkeypress event on the search field such that it will trigger a click to the searchButton. The searchButton will then reload the page but with url parameters coming from the search field. The problem is that the page reloads but the search parameters are not added to the URL when I press ENTER in the search field, but works properly when I press searchButton. Here are the codes I used:
(code added to the onkeypress of searchField)
if (typeof thisEvent == 'undefined' && window.event) { thisEvent = window.event; } if (thisEvent.keyCode == 13) { document.getElementById("#{id:searchButton}").click(); }
(code added to the onclick of searchButton)
window.location.href = "test.xsp?search=" + document.getElementById("#{id:searchField}").value;
I tested it in IE and Firefox, both have problems. I created a sample HTML file and it worked correctly. Is this a bug of XPages or am I missing something here?
Add this after your '.click()':
thisEvent.preventDefault();
thisEvent.stopPropagation();
It should solve the problem ;-)
Changing the onKeyPress event of the input field to
if (typeof thisEvent == 'undefined' && window.event) { thisEvent = window.event; }
if (thisEvent.keyCode == dojo.keys.ENTER)
{
dojo.byId("#{id:searchButton}").click();
thisEvent.preventDefault();
}
should be sufficient to solve the problem. Note that for cross browser compatibility I've used the
dojo.keys.ENTER
and
dojo.byId("id");
property/ method. The dojo.keys object has a lot more properties to check for certain key presses: see here
Mark
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With