views:

234

answers:

4

The first click expands the <option>,

the second click select one of them.

How to catch the second click?

A: 

If the selection has changed, the onchange method is called.

Gumbo
What if user clicks one the same option for second click?I don't want to miss this case.
Shore
I am also struggling with that, Shore, and from what I found I don't know if you can do anything about it :( I'll be watching this question though.
Daniel Huckstep
Seems can't do anything about it
Shore
@Shore: I don’t think that it’s possible neither. Maybe you should implement such a selection element on your own.
Gumbo
Also, don't forget that IE won't fire onchange until the element loses focus...
TM
I updated my answer to include a link to a post where the problem of selecting the same answer is solved.
T B
A: 

You could always use onclick on each option in the combobox. It's not pretty, but it should work:

<select>
    <option onclick="alert('May');">May</option>
    <option onclick="alert('June');">June</option>
    <option onclick="alert('July');">July</option>
</select>
Pete OHanlon
+1  A: 

jQuery version of Pete's answer which I think would satisfy the question asked

$("option").click(function(){
   alert($(this).text());
});

EDIT

Due to the above not being cross browser, I did some searching and came across a post here on stack overflow that might help out.

http://stackoverflow.com/questions/898463/fire-event-each-time-a-dropdownlist-item-is-selected-with-jquery

T B
This doesn't appear to be cross-browser compatible. Tested in Chrome 3.0, IE6 and Firefox 3.5. It only worked in Firefox for me.
Ben Koehler
it might be the `.text()`, you might have to try `.html()`
geowa4
The alert function doesn't get called at all in IE6 or Chrome 3.0, so changing it to .html() makes no difference.
Ben Koehler
try alerting with other text appended--`alert("text: " + $(this).text())`--to see if the event handler is even executing.
geowa4
+2  A: 

You can chain some events using jQuery's one function like this:

$('select').one('click', firstClick);

function firstClick(evt) {
    //do anything you might want to do
    $(evt.target).parent().one('click', secondClick);
}

function secondClick(evt) {
    //again, do you thing
    $(evt.target).parent().one('click', firstClick);
}

The one function will execute the event handler you give once and then remove it. This solution allows you to switch the handler on every click. Just be careful if you use this one. You also need to handle the blur event to reset the first handler.

You could also use jQuery's live function to minimize the number of event handlers you are creating, saving memory. This will also avoid some problems I could foresee with my first solution.

$('select option').live('click', selectHandler);

function selectHandler(evt) {
    //insert magic here
}
geowa4
Very clever:)
Shore