2

Why doesn't the following code work?

$('select option').live('click', function(){
  alert('hello')
})

html:

<select class="ok">
<option>5</option>
</select>

EXAMPLE: http://jsfiddle.net/fQxj7/2/

3
  • Your example fiddle works fine for me... I'm using Firefox 5 on Ubuntu 11.04. What are you using? Commented Aug 16, 2011 at 14:55
  • 1
    While FishBasketGordo's approach works, this still begs the question as to why yours doesn't. It works on FF and "click" is a valid event for <option>. Commented Aug 16, 2011 at 14:59
  • @Spycho - yes it worked with Firefox 5 to top, but not work for Chrome.!!!!!!? Commented Aug 16, 2011 at 17:25

1 Answer 1

5

Do this instead:

$('select').change(function(){
    alert('hello');
});

If you need to know the selected value inside the event handler, then you can do this:

$('select').change(function() {
    var selectedOption = $(this).val();
});

http://jsfiddle.net/FishBasketGordo/2ABAh/

Sign up to request clarification or add additional context in comments.

6 Comments

That's a much better approach, but it doesn't explain why the OP's present solution doesn't work. Does their fiddle work for you? It does for me...
I don't believe that click events get fired for <option> tags across all browsers.
I agree, but I believe it is supposed to.
If I'm reading this correctly, it's not supposed to in HTML 4.01 (w3.org/TR/1999/REC-html401-19991224/interact/…), but it is in HTML5 (w3.org/TR/html-markup/global-attributes.html).
please give me example wit HTML5?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.