2

Primefaces 5

I have following example:

<script>

function init () { 
  $("#myspan").doSomething;
}
</script>

<h:form rendered="some condition">
  <span id="myspan" />

</h:form>

How to automaticaly call init() every time form will be updated ?

2 Answers 2

6

To rerun a script, simply re-render the tag that calls it. Assuming the form is re-rendered whenever it is "updated", this will do:

<script>

function init () { 
  $("#myspan").doSomething;
}
</script>

<h:form rendered="some condition">
  <script type="text/javascript">init()</script>
  <span id="myspan" />

</h:form>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I've also posted alternative solution for this problem.
5

Alternative solution would be to use <p:remoteCommand autoRun='true'>

<script>

function init () { 
  $("#myspan").doSomething;
}
</script>

<h:form rendered="some condition">

  <span id="myspan" />
  <p:remoteCommand autoRun="true" oncomplete="init();" />
</h:form>

In the solution of Ali the <script> tag is called earlier as the solution with <p:remoteComand>. The remoteCommand is called if output is already shown to user.

2 Comments

This is actually a very good solution! The advantage is that the remote command will be executed later than the script tag solution . In my case the width of elements i had to work with was still zero. The browser called the javascript before the elements were ready. If you use the remote command, elements will be shown to the user and therefore ready to get and manipulate dimension values.
This can also be undesirable - in my case I wanted it executed as soon as possible to avoid glitch visual effects. Depends on the application.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.