2

I have a simple xpage with 2 inputText field (username and password) and a button.

The idea is that when hitting the "return" or "enter" key , that the onclick event of the button will be activated. I guess this is normally done with an auto onfocus, but can't find a way to let this work.

Marc Jonkers
  • 496
  • 1
  • 7
  • 17

2 Answers2

1

I think best is to define a small function from a js library you load in each page you need this

function fireButton(e, buttonId) {
    if (typeof e == "undefined" && window.event) {
        e = window.event;
    }

    if (e.keyCode == dojo.keys.ENTER) {
        dojo.byId(buttonId).click();
        e.preventDefault();
    }
}

Then in the onkeydown attribute you can call it:

<xp:inputText onkeydown="fireButton(event, '#{id:buttonId}')"
shillem
  • 1,260
  • 7
  • 12
  • I can't get it to work, but that's probably me ...I made a script library (gave it the same name as the function) Then added "import fireButton" at onclientload of the page .Then added "fireButton(event, '#{id:button1}') in the onkeydown of the second inputtext field , but it doesn't work – Marc Jonkers Feb 09 '18 at 11:55
  • How did you declare the library in the page? Have you opened the browser developer console to see where it gets cranky about? – shillem Feb 09 '18 at 12:12
  • I get :"Uncaught ReferenceError: fireButton is not defined at HTMLInputElement.onkeydown" , so it seems the function is not loaded ? How do I do this properly ? – Marc Jonkers Feb 10 '18 at 13:38
  • Sorry , my mistake. I saved it as a serverside javascript library instead of client side. It works perfect now ! (I also changed the name so that the name of the library and function are not the same ) – Marc Jonkers Feb 13 '18 at 08:41
0

From the Button's "All Properties" tab, set type to submit,

as shown in this screenshot.

Sam Sirry
  • 631
  • 5
  • 22
  • Setting the `type` on the first properties tab doesn't seem to work properly, as it affects the properties of the event handler (onclick) attached to the Button, not the Button itself... weird. – Sam Sirry Jun 30 '22 at 16:36