1

I have an input box which invokes a button action when Enter key is pressed.

    <h:inputText value="#{myBean.fname}" id="name" onkeydown="callClickEvent(event);"></h:inputText>


**Js function is**
--------------

function callClickEvent(event){
                 if (event.keyCode == 13 || event.which==13) {
                 document.getElementById('dataForm:btnA').click();
               }

        }

In my xhtml i have two button

<h:commandButton action="#{myBean.actionPerform}" id="btnA""/>
<h:commandButton action="#{myBean.updateAction}" id="btnB"/>

now the problem is , this code runs fine in chrome and firefox but not working as expected in IE9;

in IE 9 , when I hit enter key it invokes both buttons action.

suggested solution in stack overflow were: (1) add type ="button" (2) add e.preventDefault() inside javascipt function.

when i add type="button", action method dosen't invoke when i click second button.

and when i add e.preventDefault(), it does'nt let me type anything side input box.

any help would be appreciated.

Bifrost
  • 417
  • 5
  • 23
  • Why do you have that `callClickEvent()` in first place? It's the cause of all that trouble and doesn't look useful at all. Get rid of it and take a step back and reformulate a question for that particular problem for which you [incorrectly](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) thought that `callClickEvent()` would be the right solution. – BalusC Aug 31 '15 at 12:13
  • Hi Balu, it is onkeydown to trap enter key event. – Bifrost Aug 31 '15 at 12:40
  • it seems , enter key down submitting my form thus invoking second button action. – Bifrost Aug 31 '15 at 12:41
  • can you provide jsfiddle? – Farhan Aug 31 '15 at 12:57
  • @Farhan: no he can't. You cannot put non-parsed JSF code in a jsfiddle. And at the OP: **Why** do you want to trap the enter key event? – Kukeltje Aug 31 '15 at 13:48
  • Hi Kukeltje, text box will accept some string and just by hitting Enter key ,I want to invoke an action rather than clicking a button . – Bifrost Aug 31 '15 at 13:54
  • Is this a duplicate then: http://stackoverflow.com/questions/5485851/default-action-to-execute-when-pressing-enter-in-a-form – Kukeltje Aug 31 '15 at 13:59
  • Hi, when i add event.preventDefault(), then i am not able to type anything inside text box. function callClickEvent(event){ event.preventDefault(); if (event.keyCode == 13 || event.which==13) { document.getElementById('dataForm:btnA').click(); } } – Bifrost Sep 01 '15 at 04:49

0 Answers0