Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPages - onkeypress event not triggering click properly

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?

like image 372
John Bautista Avatar asked Feb 04 '12 23:02

John Bautista


2 Answers

Add this after your '.click()':

       thisEvent.preventDefault();
       thisEvent.stopPropagation(); 

It should solve the problem ;-)

like image 126
Ferry Kranenburg Avatar answered Nov 08 '22 07:11

Ferry Kranenburg


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

like image 6
Mark Leusink Avatar answered Nov 08 '22 06:11

Mark Leusink