I have a HTML option element that I want to perform some actions when it is selected. So I have made it react to the onmousedown event & made it perform actions on that event.
My Problem: The code only works in Firefox. In Safari the event is never triggered? The code is never executed. Do you know why this is happening? Maybe I should react to a different event such as onselect or onchange?
This is a simple example of what I am doing(I perform alot of code onmousedown but here its just an alert for simplicity). As you will see, the alert occurs in Firefox BUT it doesn’t work on Safari for some reason:
<html>
<head>
</head>
<body>
<select>
<option id="a1">1</option>
<option id="a2">2</option>
<option id="a3">3</option>
<option id="a4">4</option>
<option id="a5">5</option>
</select>
</body>
<script type="text/javascript">
<!--
document.getElementById("a1").addEventListener("mousedown", function(e) { alert("A"); return true; }, false);
document.getElementById("a2").addEventListener("mousedown", function(e) { alert("b"); }, false);
document.getElementById("a3").addEventListener("mousedown", function(e) { alert("c"); }, false);
document.getElementById("a4").addEventListener("mousedown", function(e) { alert("d"); }, false);
document.getElementById("a5").addEventListener("mousedown", function(e) { alert("e"); }, false);
-->
</script>
</html>
It looks like the onmousedown event is only fired for an option if you click on it in a listbox—a select with
multiple="true". Clicking on the option when it’s dropped down from a “regular” select doesn’t seem to fire the event.Demo
Yes, I would use the
onchangeevent:DEMO