function myfunc() { var param = 4; alert("OK"...">

How to use <a4j: jsFunction> <a4j: actionparam>

I am trying to use:

<script type="text/javascript"> function myfunc() { var param = 4; alert("OK"); } </script> 

I call the function as follows:

 <a4j:jsFunction name="myfunc"> <a4j:actionparam name="param" assignTo="#{MyBean.myfield}"/> </a4j:jsFunction> 

But that will not work. What could be the reason?

+4
source share
4 answers

You misunderstood the purpose of <a4j:jsFunction> . It auto -generates a JavaScript function that you can call from any JavaScript code in your view.

Your example

 <a4j:jsFunction name="myfunc"> <a4j:actionparam name="param" assignTo="#{MyBean.myfield}"/> </a4j:jsFunction> 

will generate the following function

 <script> function myfunc(param) { // Here some specific JSF Ajax script which assigns "param" // to a managed bean property #{MyBean.myfield} } </script> 

You do not need to define it yourself. You only need to call it from some JavaScript code elsewhere. For instance,

 <span onclick="myfunc(4)">click here to set 4 in MyBean.myfield</span> 

or

 <script> function someOtherFunction() { var param = 4; myfunc(param); } </script> 

which, in turn, is used as

 <span onclick="someOtherFunction()">click here to call someOtherFunction() which will in turn set 4 in MyBean.myfield</span> 

See also:

+8
source
 <a4j:jsFunction 

not used to call a function, it is used to define a function.

So, if MyBean.myfield is an int-field, you can set the value to 2 using:

 <script>myfunc(2);</sript> 
+3
source

There are several ways to call this function.

Two that you find particularly useful:

It:

 <body onload="myfunc();"> 

Example: http://ultimatemmo.webege.com/Test.html

and this:

 <a href="#" onclick="myfunc();">Click here to execute function</a> 

Example: http://ultimatemmo.webege.com/Test2.html

Edit:.

+1
source

According to your code snippet, you never called your function. Add myfunc(); into your script tag.

0
source

All Articles