I have a page that gets values from a DB, I can’t control the values / how they are called, but the end result looks like standard HTML code, for example:
<div id="myDiv" class="controls">
<select id="jform_province" name="jform[province]">
<option value="AB">Alberta</option>
<option value="BC">British Columbia</option>
<option value="MB">Manitoba</option>
</select>
</div>
I’m looking to put a script at the end of the page that can modify the option elements by say adding a function so it looks like this:
<option value="AB" onClick="someFunction()">Alberta</option>
How can I modify the code? I know how to edit the entire content of the div but I want to change the elements, I will not know what the values of the elements are so the script needs to find all option tags in “myDiv” (I have other lists on the page I don’t want changed) and add that code to all of them.
Thanks
Just attach an event handler:
Note that
<option>elements do not fireonclickevents when selected, and even if they did it wouldn’t detect alternate input methods such as using the keyboard. The code above provides an alternative.