How do you programmatically select items in a multi-select listbox using jQuery?
You can do it like this:
var valToSelect = "1";
$("#mySelect option[value='" + valToSelect + "']").attr("selected", "true");
Here's a quick example: http://jsfiddle.net/ZyAHr/
Just for kicks, here's an alternative example if it fits the situation:
var values = $("select").val();
values.push("1");
$("select").val(values);
Here's a quick example of this: http://jsfiddle.net/FBRFY/
This second approach takes advantage of the fact that .val()
on a multiple <select>
element returns an array, not a string. You can get it, add or remove any values, then set it again using .val()
and it'll be updated with the new selection.
In ListBox that have multi selection mode use it :
$('#ListBox1').find('option:selected').map(function () {
alert($(this).text());
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With