Peter Jaloveczki
Peter Jaloveczki

Reputation: 2089

Jquery event not called after update

I have a pretty simple JSF form on my hand like so:

<h:inputText id="number" value="#{bean.value}"
styleClass="integer" onkeyup="update()" />

I have a jQuery selector as well like this:

jQuery(document)
        .ready(
                    jQuery(".integer")
                            .keydown(
                                    function(event) {
                                        //do your stuf


        });

Now, this function is getting triggered pretty well, until I update the container around this input. It is a ui:repeat by the way. The update() method only refreshes a label. After the update the function is never triggered until I reload the page. This jquery included from a file in the head part, but is is still there after the update, function is not triggered however? How can I make it work again after update?

Upvotes: 0

Views: 658

Answers (2)

Jens
Jens

Reputation: 6383

As others have already answered: When you update the container the event handler is not attached anymore and your need to reattach it.

However, another option to this is using jquery`s on() method.

So use something like this (untested though):

jQuery(document)
    .ready(
        $(document).on("keydown", '.integer', function (event) {
            //do your stuf
    }));

Upvotes: 1

Rong Nguyen
Rong Nguyen

Reputation: 4189

This isn't work after you update because the element isn't bound event anymore, so you should create a Javascript's function to solve this issue:

<script type="text/javascript">
function test(){
jQuery(".integer")
                            .keydown(
                                    function(event) {
                                        //do your stuf


        });
};
jQuery(document)
        .ready(test();
              );      
</script>

Component update your target component should call this test() function, like:

<p:commandButton oncomplete="test();" update=":COMPONENT_ID_HERE" ..../>

Upvotes: 0

Related Questions