Sunday, February 03, 2008

Checkbox in APEX

You want to create a checkbox in Oracle Application Express and get it's value in javascript?

An example of a checkbox:
In my application I want to do an interactive search whenever the checkbox is checked.

So, how do you know if the checkbox is checked? Let's try with an example.

  1. Create a new "Check Box" item and give it a name, for ex. P26_CHECKBOX.
  2. In the List Of Values, put STATIC2: ;Y (this checkbox doesn't have a label, but will return the Y value whenever the checkbox is checked)
  3. If you look at the Session State (in the developer toolbar) of the item after a submit of the page, you see the checkbox item has a value of Y when it's checked...
  4. If you want to get the value of an item in javascript within APEX, you normally do $x('ITEM_NAME').value
  5. So let's try $x('P26_CHECKBOX').value -> it returns: undefined
  6. If you know a bit of javascript/html, you maybe think you need to use $x('P26_CHECKBOX').checked -> it returns: undefined
  7. The above doesn't work, because APEX is putting a number after the item. In my example: $x('P26_CHECKBOX_0')
  8. If you try with $x('P26_CHECKBOX_0').value, it return Y or with $x('P26_CHECKBOX_0').checked, it returns true... and that's what we'd like to have
  9. See it in action here
If you've more checkboxes (because the lov is returning more values) you can use following function to see what's checked:

2 comments:

Paul said...

Dimitri,

Is it possible to use this code in a report that uses apex_item.checkbox in SQL query and then pass the apex_application.g_f10 as the checkbox value?

Dimitri Gielis said...

Hi Paul,

I missed your comment. I guess it will work yes, it's just javascript. If you have a unique name it should work. Try it and tell me ;-)

Dimitri