3

I would like to know how can I use jQuery to select multiple items without having to press the SHIFT button on my keyboard in a simple html dropdown element:

<select>
<option></option>
<option></option>
<option></option>
</select>

thanks

1
  • To be clear here, you want to break standard UI interface standards? Commented Feb 25, 2010 at 13:32

1 Answer 1

4

Given:

<select multiple="multiple" id="foo">
<option>1</option>
<option>2</option>
<option>3</option>
</select>

Do this:

$('#foo option').attr('selected', 'selected');

That would select all of them. That's the best I can give you, without a more specific question.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks sorry what's the difference between $(this).toggleClass("selected"); and $("option.selected").attr("selected", "selected") ? They both add a class to the element, don't they ?
The former, run on an <option> tag, will add or remove the "selected" class, as in <option class="selected">. The attr() function will add the "selected" attribute, as in <option selected="selected">. The class has no bearing on whether or not the option is marked as selected, as far as form submission is concerned. Also note that $('option.selected') means "any <option> tag with the class "selected"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.