jqueryjquery-uibuttonjsf-1.2

How to trigger input type button by pressing Enter key


In JSF 1.2 and I'm using h:commandButton type="button" but the button does not trigger when I tab on it and press enter. I tried using onkeypress="keyEnter(event)"

     function keyEnter(eve){
         if (eve.keyCode == 13) {
              document.getElementById(eve.target.id).click();
          }
          return false;
      }

and it triggers the button when enter key is pressed. Now here is my question, I have many h:commandButton elements with type button, what can I do in order to implement keyEnter(eve) to all the elements of type button?

Thanks in advance.


Solution

  • Try this

    FULL CODE

    function keyEnter(eve){
        var key = eve.keyCode || eve.which;
         if (key == 13) {
             document.getElementById(eve.target.id).click();
         }
          return false;
    };
    

    To bind all You can do this

    $('input[type="button"]').on("keyenter",function(eve){
         var key = eve.keyCode || eve.which;
         if (key == 13) {
              $(this).click();
          }
          return false;        
    });