5

I'd like to execute valueChangeListener for p:inputText when user changes text and inputText looses focus (onchange). Is this possible? For now it only executed after I press return.

Danijel
  • 8,198
  • 18
  • 69
  • 133

1 Answers1

20

The valueChangeListener method requires a form submit to be invoked. This is a server side event, not a client side event or so. Just changing and blurring the input does by default not submit the form at all. Bring in a <p:ajax> to do the magic.

<p:inputText value="#{bean.inputValue}" valueChangeListener="#{bean.inputChanged}">
    <p:ajax />
</p:inputText>

However, although you didn't tell anything about the concrete functional requirement for which you thought that this is the right solution, I just wanted to mention that the valueChangeListener is more than often the wrong tool for the job you had in mind. Use <p:ajax listener> instead.

<p:inputText value="#{bean.inputValue}">
    <p:ajax listener="#{bean.inputChanged}" />
</p:inputText>

Note that this would then make it possible to pass method arguments by EL 2.2, which would immediately then answer the possible underlying functional requirement of your other — actually pretty poor — question.

<p:inputText value="#{bean.inputValue}">
    <p:ajax listener="#{bean.inputChanged('arg1', 'arg2')}" />
</p:inputText>

Also note that this might not be the solution at all if you're actually interested on the entered value; you could just access the inputValue property directly.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555