How do you programmatically select items in a multi-select listbox using jQuery?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can do it like this:
Here’s a quick example: http://jsfiddle.net/ZyAHr/
Just for kicks, here’s an alternative example if it fits the situation:
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.