I'm trying to add custom style to a <select> element and because of cross-browsers limitation I would either need to know:
- When a select dropdown is expanded (I did a quick Google search and it didn't seem possible)
- Add events on
<option>elements
The second option seems to work in all browsers but Chrome. I would prefer to avoid doing some nasty browser detection so I'm looking for ways to solve either one of the two options if anyone has suggestion??
The following works in all browsers but Chrome:
<select id="gender" name="gender">
<option></option>
<option class="click">Male</option>
<option>Female</option>
<option>Other</option>
</select>
<script>
element = document.getElementById('gender');
for (var iterator = 0; iterator < element.options.length; iterator++) {
var optionElement = element.options[iterator];
if (optionElement.className == 'click') {
optionElement.addEventListener('click', function() {alert('gender')})
}
}
</script>
The reason I cannot use an event on <select> is because I need to know if the dropdown is expanded or not (point #1 above). I also considered using the onChange event but browsers seems to react differently to it. Some react when you click, other when you change the value.