In a dropdown I want to get a custom attribute from my options element:
<select name="t" id="t">
<option value="all">- all -</option>
<optgroup label="Europe">
<option selected value="1" myAttr="1">Fr</option>
<option value="2" myAttr="0">Uk</option>
</optgroup>
<optgroup label="America">
<option value="3" myAttr="1">USA</option>
<option value="4" myAttr="0">CA</option>
</optgroup>
<optgroup label="Asia">
<option value="5" myAttr="0">JP</option>
</optgroup>
</select>
So I first loop over optgroup
tags, and then I loop over option
one's (Fiddle).
var temp = document.getElementById("t").childNodes;
// Loop the optGroup
for (var i = 0; i < temp.length; i++) {
// Loop the option
for (var j = 0; j < temp[i].childNodes.length; j++) {
Then I want to test if my element is an HTMLOptionElement before getting my custom attribute, I tried some tips from this SO question, but without success, I always get 'undefined'. Is thhre a specific method to use with DOM elements?