0

I am surprised to see that the event valueChange does not trigger when the value of an inputText is changed by a javascript method. However, it triggers when I manually change the value of the inputText.

You can test the code below to sense this, and maybe you can give a solution to make the event to trigger when a javascript function changes the value of a component.

The xhtml page:

<html xmlns="http://www.w3.org/1999/xhtml"
   xmlns:ui="http://java.sun.com/jsf/facelets"
   xmlns:f="http://java.sun.com/jsf/core"
   xmlns:c="http://java.sun.com/jsp/jstl/core"
   xmlns:h="http://java.sun.com/jsf/html"
   xmlns:p="http://primefaces.org/ui"
>
  <body>
    <script type="text/javascript">
      function testValueChange() {
        console.log("testValueChange called");
        var inputHCodes = document.getElementById('form:inptHCodes');
        inputHCodes.value = "new value";
      }
    </script>
    <h:form>
      <p:commandButton type="button" value="BUTTON" onclick="testValueChange();"/>

      <h:inputText id="inptHCodes" value="#{questionnaireExtendedKeyAttribute.selectedInputCodes}">
        <p:ajax event="valueChange" listener="#{questionnaireExtendedKeyAttribute.testValueChangeListener}"/> 
      </h:inputText>
    </h:form>
  </body>
</html>

The listener in backing bean:

public void testValueChangeListener(AjaxBehaviorEvent abe) {
   UIInput uiinput = (UIInput) abe.getSource();
   String id = uiinput.getId();
   selectedInputCodes = uiinput.getValue().toString();
   logger.debug("### value : " + selectedInputCodes + " and id is: " + id);
}
Nelly Junior
  • 556
  • 2
  • 10
  • 30
  • Does it work with f:ajax instead of p:ajax? If not, it is not PrimeFaces related (and I'm sure there is a duplicate of this Q in stackoverflow. If I remember correctly it is a html issue where this event is not triggered via javascript. Maybe fire it explicitly. – Kukeltje Oct 12 '16 at 20:00
  • It doesn't work with f:ajax. How to fire it explicitly? – Nelly Junior Oct 12 '16 at 20:14
  • I am looking here http://stackoverflow.com/questions/7221495/pass-parameter-to-premotecommand-from-javascript, but I can't get it... – Nelly Junior Oct 12 '16 at 21:05
  • That question is not related. Look for jquery trigger()... – Kukeltje Oct 13 '16 at 09:55
  • It is about triggering an ajax event, not jQuery. But I tried to trigger it explicitly and it does't work. – Nelly Junior Oct 13 '16 at 10:48
  • PrimeFaces components use jquery event handling to trigger sending ajax requests..but these are dom plain html components. Then see how you can 'trigger' those.. And how did you trigger it? But these are – Kukeltje Oct 13 '16 at 13:11

0 Answers0