Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9155735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:38:58+00:00 2026-06-17T12:38:58+00:00

I am registering a change event listener for every input in the HTML using

  • 0

I am registering a change event listener for every input in the HTML using jQuery as below:

<h:head>
    <script type="text/javascript">
        //<![CDATA[
        $(document).ready(function(){
            $(':input').each(function() {
                $(this).change(function() {
                    console.log('From docReady: ' + this.id);
                });
            });
        });

        function changeHandler() {
            console.log("from changeHandler");
        }
        //]]>
    </script>
</h:head>
<h:body>
    <h:form id="topForm">
        <p:commandButton id="myButton" value="update"
                         action="#{testBean.submit}"
                         partialSubmit="true" process=":myForm:panel"
                         update=":myForm:panel" />
    </h:form>
    <h:form id="myForm">
        <p:panel id="panel">
            <p:inputTextarea id="myTextarea"
                             value="#{testBean.val}"
                             onchange="changeHandler();"/>
        </p:panel>
    </h:form>
</h:body>

Both change events are being triggered if the user changes the content of myTextarea. However after pressing the update button, which partially updates the myTextarea, only the changeHandler is triggering afterwards. The event bound in $(document).ready() is not triggering anymore.

Is this PrimeFaces related and/or an expected behavior? If yes then how can I ensure to trigger the second event without rerunning document ready script again.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:38:59+00:00Added an answer on June 17, 2026 at 12:38 pm

    As to the cause of your problem, the ajax request will update the HTML DOM tree with new HTML elements from the ajax response. Those new HTML elements do —obviously— not have the jQuery event handler function attached. However, the $(document).ready() isn’t re-executed on ajax requests. You need to manually re-execute it.

    This can be achieved in various ways. The simplest way is to use $(document).on(event, selector, function) instead of $(selector).on(event, function). This is tied to the document and the given functionRef is always invoked when the given eventName is triggered on an element matching the given selector. So you never need to explicitly re-execute the function by JSF means.

    $(document).on("change", ":input", function() {
        console.log("From change event on any input: " + this.id);
    });
    

    The alternative way is to explicitly re-execute the function yourself on complete of ajax request. This would be the only way when you’re actually interested in immediately execute the function during the ready/load event (e.g. to directly apply some plugin specific behavior/look’n’feel, such as date pickers). First, you need to refactor the $(document).ready() job into a reusable function as follows:

    $(document).ready(function(){
        applyChangeHandler();
    });
    
    function applyChangeHandler() {
        $(":input").on("change", function() {
            console.log("From applyChangeHandler: " + this.id);
        });
    }
    

    (note that I removed and simplified your completely unnecessary $.each() approach)

    Then, choose one of the following ways to re-execute it on complete of ajax request:

    1. Use the oncomplete attribute of the PrimeFaces command button:

      oncomplete="applyChangeHandler()"
      
    2. Use <h:outputScript target="body"> instead of $(document).ready(),

      <h:outputScript id="applyChangeHandler" target="body">
          applyChangeHandler();
      </h:outputScript>
      

      and reference it in update attribute:

      update=":applyChangeHandler"
      
    3. Use <p:outputPanel autoUpdate="true"> to auto update it on every ajax request:

      <p:outputPanel autoUpdate="true">
          <h:outputScript id="applyChangeHandler">
              applyChangeHandler();
          </h:outputScript>
      </p:outputPanel>
      
    4. Use OmniFaces <o:onloadScript> instead of $(document).ready(), <h:outputScript> and all on em.

      <o:onloadScript>applyChangeHandler();</o:onloadScript>
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm registering a preference change listener like this (in the onCreate() of my main
I am building an application where most of the HTML is built using javascript.
When registering an event via addEventListener on an element, then delete that element without
I am registering java script to my Asp.net code behind file, which is working
Using the method described in the MSDN for registering a Windows Service (ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.WIN32COM.v10.en/dllproc/base/createservice.htm) and
I am registering a lifecycle listener in the plugin.xml . It runs ok if
Problem I am registering a keyup event that, when activated, shows a list of
How do I change the order of the url parameter when registering routes in
I want to change Checkbox value without raising the onCheck event. I know there
Am I doing something wrong? Why isn't the click event registering on the first

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.