views:

365

answers:

3

I have an empty select element, I want to execute some code when that element is clicked on, but I do not want the dropdown list to appear, here is what I have found:

  1. When I disabled the select element I cannot catch the mousedown event.
  2. When I enable the select element I can capture the mousedown event but the dropdown list appears.

For arguements sake what I want is to be able to click the select element, JavaScript to throw an alert box but I want to prevent the dropdown list from displaying.

Thanks!

A: 

Why not to disable a select element and wrap the select element in a div (or something else) of the same height and width as the select element and use mousedown event with this div?

TomaszO
Hmmm... will that work as the select will have a higher z-index compared with the select? I will try now, thanks for your answer :)
ILMV
No this hasn't worked, like I thought the div being behind the disabled select box doesn't pickup the click event.
ILMV
+1  A: 

capture onmousedown event of select element and cancel the event by setting event.returnValue to false.

<script type="text/javascript">
    function onSelectMouseDown() {
        event.returnValue = false;
        alert("mouse down caught and cancelled");
    }
</script>

<select id="select" onmousedown="onSelectMouseDown();"></select>
gp
Ok this is looking more promising, I will have a play around and return when I'm done :-)
ILMV
After making sure I got my JavaScript events correct this worked like a charm! Many thanks.
ILMV
A: 
<script>
function makeDisable(){
    var x=document.getElementById("mySelect")
    x.disabled=true
}
function makeEnable(){
    var x=document.getElementById("mySelect")
    x.disabled=false
}
function f() {
makeDisable();
alert("something");
t=setTimeout("makeEnable();",1);
}
</script>

<select id="mySelect" onMouseDown="f();">
  <option value="opt1">Option 1</option>
  <option value="opt2">Option 2</option>
</select>
TomaszO