simulating mouse click on select option with jquery

R3lm picture R3lm · Apr 25, 2012 · Viewed 19.4k times · Source

When I click on a Select option, I am handling the Changed event.

I want to simulate this in code, and have it LOOK THE SAME as when the user clicks on the option.

So far, setting 'selected' attribute on the option does not highlight it the same as when you click on it.

I can trigger 'change' on the option and hit my handler, but the option on the list is not selected as if clicked.

Suggestions?

UPDATE: The query selection code works just fine - thanks for the responses. The problem was mine (of course). I had the selection code outside the Jquery.Ajax Success: block, so I think it was working, but the ajax response code was hosing it.

Answer

Ben Foster picture Ben Foster · Apr 25, 2012
​$('option[value="4"]').attr('selected', 'selected').parent().focus();​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

Works for me. See http://jsfiddle.net/TZVyh/1/