In this tutorial, you will learn how to execute JavaScript code using a dynamic action in Oracle Apex.
You can execute the JavaScript code directly through the dynamic action, or you can declare the JavaScript function on the page and then call it using the dynamic action. Both examples are given below:
Dynamic Action Execute JavaScript Code
In the following example, on a button click, it will sum up the values entered in page item P1_ITEM and P1_ITEM1 and will show the total through the alert message.
Create a dynamic action on a button and set the following properties:
- Action: Execute JavaScript Code
- Code: add the following code to it:
var nTot; nTot = parseFloat(apex.item("P1_ITEM").getValue(),10)+ parseFloat(apex.item("P1_ITEM1").getValue(), 10); apex.message.alert("Total: "+nTot);
Output
But what if there is a need to execute the same JavaScript code from many other events? Obviously, you should create it as a function and then call it in different events using the dynamic action. Below is an example:
Calling JavaScript Function from Dynamic Action
Click on your page and in property pallet scroll down to Function and Global variable declare section and add the following code in it:
function calcTot() { var nTot; nTot = parseFloat(apex.item("P1_ITEM").getValue(), 10) + parseFloat(apex.item("P1_ITEM1").getValue(), 10); apex.message.alert("Total: "+nTot); }
Now instead of executing the whole JavaScript code through dynamic action, add only the JavaScript function name in the Code section of the dynamic action. Below is the example:
calcTot();