Home > Adding Application Components > Incorporating JavaScript in... > Referencing Items Using Jav...
When you reference an item, the best approach is to reference by ID. If you view the HTML source of an Oracle Application Express page in a Web browser, you would notice that all items have an id attribute. This id corresponds to the name of the item, not the item label. For example, if you create an item with the name P1_FIRST_NAME
and a label of First Name
, the ID will be P1_FIRST_NAME.
Knowing the item ID enables you to use the JavaScript method getElementById()
to get and set item attributes and values. The following example demonstrates how to reference an item by ID and display its value in an alert box.
<script language="JavaScript1.1" type="text/javascript"> function firstName(){ alert('First Name is ' + document.getElementById('P1_FIRST_NAME').value ); } // or a more generic version would be function displayValue(id){ alert('The Value is ' + document.getElementById(id).value ); } </script> // Then add the following to the "Form Element Attributes" Attribute of the item: onchange="displayValue('P1_FIRST_NAME');"