Oracle Apex Set and Get Item Value Using Javascript

1. Introduction

Today most oracle apex applications contains Javascript code, particularly useful for making forms more interactive and user friendly.

In this article, we’ll see how to set item value and get item value using Javascript in Oracle Apex.

2. Set Item Value Using Javascript

To set the page item value in apex we need to use $S function of the javascript.

The $ function can be used to set the value of Text item, Display only item or also hidden item in Apex.

The following examples shows the simplest way to set value of the page item in javascript.

$s("P2_TEXT_FIELD","Hello World!");
$s("P2_DISPLAY_ONLY","Hello World!");

There are few other ways to set the item values as well, shown below:

$x("P2_TEXT_FIELD").value = "Hello World!";
$("#P2_DISPLAY_ONLY").text("Hello World!");

To set the value using the Apex JavaScript API function:

apex.item( "P2_TEXT_FIELD" ).setValue ("Hello World!");

3. Get Item Value using Javascript

To get the page item value in apex, we need to use $ or $x function of the javascript.

The $ function can be used to get the value from Text item, Display only item or also hidden item in Apex.

The following example shows the simplest ways to get the value of the page item in Javascript.

x = $("#P2_DISPLAY_ONLY").text(); //To get the text information of the item, simple $ function is used. 
$x("P2_TEXT_FIELD").value; // To get the value of the text field, $X is used.

To get the page item values, we can also simply use the $v(“<item_name>”).

x = $v("P2_TEXT_FIELD");
y = $v("P2_DISPLAY_ONLY");

To get the value using the Apex JavaScript API function:

apex.item( "P2_TEXT_FIELD" ).getValue();

To access the multiple items in an array, $v2() comes handy.  For example multiple selections from check boxes, the selected values can be accessed by using:

myArr = $v2("P2_AREAS_OF_INTEREST");
for (idx=0; idx<myArr.length; idx++) {
  //do something with myArr[idx];
}

4. Conclusion

To summarize, we’ve discussed the different way available in Oracle Apex to set page item value and get page item value.

FAQs

How to get multiple items in Array using Javascript in Oracle Apex?

To access the multiple items in an array using javascript in oracle apex, $v2() function can be used.