Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use Jquery to find and select a Multi Select option by the Value?

I can't seem to figure out how to correctly select a Multi Select option by the Value and leave the other options that are selected alone.

Update It does work, thanks! I had the multi-select hidden and I thought firebug would update the option to "selected" but it doesn't. When I "show" the multi-select box after setting the attr to selected, it was selected. So that was also part of my problem, what firebug was showing me behind the scene.

like image 585
Breadtruck Avatar asked Mar 09 '10 11:03

Breadtruck


2 Answers

To select an individual option, leaving the rest alone:

$("#selectID option[value='" + myValue + "']").attr('selected', 'selected');

Or, alternatively since .val() returns an Array in the multiselect case:

var vals = $("#selectID").val();
vals.push(myValue);
$("#selectID").val(vals);
like image 185
Nick Craver Avatar answered Nov 06 '22 15:11

Nick Craver


You can use the following to find and select multi select dropdown

 $('#selectID ').children("option[value=" + myValue + "]").prop("selected", true);
like image 2
Athul Nalupurakkal Avatar answered Nov 06 '22 15:11

Athul Nalupurakkal