5

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
John Bautista
  • 1,480
  • 3
  • 29
  • 60

4 Answers4

7

Add this after your '.click()':

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

It should solve the problem ;-)

Ferry Kranenburg
  • 2,625
  • 1
  • 17
  • 23
  • Yes, this is correct - the problem lies in the enter keypress is actually going on to submit the form to the server - the page redirect can't happen at that point. – Jeremy Hodge Feb 05 '12 at 16:07
6

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

Mark Leusink
  • 3,747
  • 15
  • 23
0

I've done this just recently in an XPage, and the following script works for me cross-browser:

var e = arguments[0] || window.event;
if ( e.keyCode==13 || e.which==13) {
  window.location.href = 'searchResults.xsp?query=' + 
      encodeURI(dojo.byId('#{id:searchInput}').value));
  return false;
}

Hope this helps,

Jeremy

Jeremy Hodge
  • 1,655
  • 9
  • 8
  • Thanks for the answer. But I think my question is about the onkeypress event triggering the button click, but not doing the reloading (with URL parameters) correctly. – John Bautista Feb 05 '12 at 02:37
  • Ok, Im still not sure then what you are asking - does it not trigger the script, or does it not reload the page ? – Jeremy Hodge Feb 05 '12 at 14:24
  • actually what's weird is that it triggers the click on the button, but the event on the button is not executed (the reloadin/redirecting). The redirecting event only works when the button is manually clicked. :( – John Bautista Feb 05 '12 at 14:47
0

Issue is there with the id, generated by xpage. I had a same issue. xPages prefix the id of custom control like view:_id1:_id... Try by giving complete id