tags:

views:

246

answers:

1

hi,

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

+3  A: 

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.

Adam Backstrom
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 ?
Patrick
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"
Adam Backstrom