Tony
Tony

Reputation: 2406

Automatically call javascript on update of some component in PrimeFaces

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 ?

Upvotes: 2

Views: 7000

Answers (2)

Tony
Tony

Reputation: 2406

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.

Upvotes: 5

Ali Cheaito
Ali Cheaito

Reputation: 3856

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>

Upvotes: 6

Related Questions