views:

491

answers:

2

For Internet Explorer only it seems that the target (srcElement) of clicks,mousedowns,mouseups,mouseovers,etc on <select /> elements will not be tied to the <option /> element.

Given the following HTML:

<select id="madness">
   <option value="1">One</option>
   <option value="2">Two</option>
   <option value="2">Three</option>
</select>

and this basic jQuery event handler:

$("#madness").mousedown(function(e){
   alert(e.target.value); //jQuery pushes srcElement into the target for us here
});

The object that is in e.target or e.srcElement is always equal to the <select /> and not the <option />.

I decided to get creative and try to use document.elementFromPoint(e.clientX,e.clientY) and that returns the SELECT element as well.

That brings the question, is there any way to determine the <option />inside of a <select />via event arguments,coordinates, or anything else? I'm aware that I can fake this out with a scrollable div of checkboxes. For the purposes of this question, I'd like any solutions which can use the native select element.

+2  A: 

Put simply there are no solutions that use the native select element.

AnthonyWJones
The IE Team don't seem to think so: "In IE7 however, we re-implemented the <SELECT> element to make IE7 more standards-compliant. This new version *does not use any Shell controls any more*. In fact, *it is implemented totally through the MSHTML framework*, including styling, UI interaction, and rendering" - http://blogs.msdn.com/ie/archive/2006/01/17/514076.aspx (emphasis added)
NickFitz
@NickFitz: Point taken, misleading text removed. However it seems unlikely they would have started from scratch. I can see the sense of bring it inside MSHTML since it eliminates some DLL-HELL.
AnthonyWJones
It's a little disappointing, but this was where I suspecting this was going. I just wanted to see if there were other ideas.
Josh Bush
A: 

You can do:

$("#madness").val()

To get the value of the selected <option />.

Willson Haw
On mousedown and hover events, there is no selected option.
Josh Bush
Are you trying to just select the <option> into a jquery object? You can do $("#madness").children() to get all the <option> values and then you can use a $.each() to loop through them all.
Willson Haw
No, I'm actually trying to get only the option underneath the mouse.
Josh Bush