Getting and setting Apex page item values using $v(), $s(), $v2()

January 11, 2013 at 2:06 pm | Posted in Oracle Application Express (Apex), Oracle Developement | 13 Comments
Tags: , , ,

The Apex JavaScript API has some very convenient functions to access the values of page items.

For example, if you wante to access the contents of a text field with JavaScript, would would need to reference it something like this:

$x("P2_TEXT_FIELD").value;

If the item you want to reference is a display only item, then the syntax changes:

x = $("#P2_DISPLAY_ONLY").text();

If you need to set the values of these items, the you need to use the varying syntax as well:

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

or

$("#P2_DISPLAY_ONLY").text("Hello World!");

Dealing with these various syntax constructs can be confusing. Fortunately the Apex JavaScript API makes this process much easier. To get page item values simply use $v(“<item_name>”):

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

To set the item values use:

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

See an example on my demo page.

The $v2() is handy if you need to access multiple items in an array. For example multiple selections from check boxes or from a shuttle control can be fetched as an array and handled that way in JavaScript:

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

An example of this functionality can be seen on my demo page, where I also compare $v() and $v2() when used in an array.

About these ads

13 Comments »

RSS feed for comments on this post. TrackBack URI

  1. Hi Christoph,

    to set Items better use $s instead of v$, must be a copy/paste typo ;)

    Cheers,
    ~Dietmar.

    • Thanks for the correction, Dietmar. It helps to have another pair of eyes check your work. :)

  2. And what about the item’s attributes like display only, hidden, etc. What syntax is used in this case to set these attributes?

    • David,
      display only items and hidden items work the same way. You can use the Firebug console to test this out.

      Cheers,
      Christoph

  3. Hi i am new in Apex and my question is now, when i create just a button with the following script:
    javascript:$s(“P2_TEXT_FIELD”,”Hello World!”);
    its not work , what do i wrong ?

    • Arni,
      i’m not sure what you’re doing wrong. If you have a button named P2_BUTTON, then you can alter the button text with: $s(“P2_BUTTON”,”hello”). This would change the button text to “hello”.
      You can easily test this in the Firebug console in Firefox (if you don’t have the Firebug add-on for Firefox, get it.).

  4. You can simply create a button based on a dynamic action. Use a set value action to set the page item.

  5. Hi Christopher,
    Not sure how to do that.
    I have been creating a PL/SQL process on the button click and setting the value there. But i dont think thats the nice way of doing it, I want to do it in javascript onclick of the button. I am sure we can do it and I am missing something…just dont know what :(

    • Vidya,
      dynamic actions execute javascript and/or pl/sql declaratively, i.e. without having to write code.

        Right click your button and select ‘Create Dynamic Action’
        Enter a name for the Dynamic Action, e.g. ‘Set Hidden Item Value’. Click Next.
        Leave the defaults (Event=click,selection type=button,Button=,condition=no condition). Click Next.
        Set the Action to ‘Set Value’. Then a new region shows named ‘Settings’.
        Uncheck the ‘Fire on Page Load’ check box.
        Select the Set Type to ‘Static’. (you can also choose one of the other types and code accordingly).
        Set the value to ‘2’. Click Next.
        Set the Selection Type to ‘Item(s)’, then a shuttle control pops up. Choose P22_ADD_VAL and move it to the right side of the shuttle. Click Create Dynamic Action.

      Now whenever you click the button, P22_ADD_VAL will be set to ‘2’.
      It is done with JavaScript in the background, and no manual coding is necessary.

  6. Thank you christoph. Will test that and keep you updated.

  7. This is great, but how would I use it to retrieve the specific SeriesName from a APEX flash Chart, after the user clicks on it ?

  8. hi
    thank you


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com. | The Pool Theme.
Entries and comments feeds.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: