Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simulating mouse click on select option with jquery

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.

like image 808
R3lm Avatar asked Apr 24 '12 23:04

R3lm


2 Answers

​$('option[value="4"]').attr('selected', 'selected').parent().focus();​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

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

like image 123
Ben Foster Avatar answered Oct 26 '22 20:10

Ben Foster


I was working with the jquery datatable plugin ColumnFilterWidgets and I needed to do the following to execute the filter action (based on answers from elclanrs and Ben Foster):

$("option").attr('selected', 'selected').parent().focus();
$("option").parent().change();
like image 32
Mark Avatar answered Oct 26 '22 19:10

Mark