Search code examples
xpagesxpages-ssjs

set focus on a button


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.


Solution

  • 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}')"