JQuery selectors: multiselect vs select

Is it possible to select multi-selection through jQuery for selection, otherwise?

The problem I am facing is that I have a generic js call that does something with an identifier. However, on some pages this has a different purpose.

Therefore, I would like to know the difference.

$("select#categories[multiselect]").doOneThing; //multiselect $("select#categories").doAnotherThing; //normal single select 

Possible?

+7
javascript jquery css-selectors
source share
3 answers

The correct attribute name for the <select> element with multiple selectable options is multiple. You can use the has attribute selector to select items with multiple attributes and combine it with: not () to select items that allow only one selection.

Therefore, your jQuery selector should be:

 $("select#categories[multiple]") // <select> with multiple $("select#categories:not([multiple])") // <select> with single only 

http://www.w3.org/TR/html401/interact/forms.html#edef-OPTION

+16
source share

If you have something like this (XHTML):

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

You can create a selector that validates the multiple attribute. Try something like this:

 $('select#categories[multiple="multiple"]').doOneThing(); 
0
source share

If you want to apply to all multiple selections on a page, just use this:

$('select[multiple]')

0
source share

All Articles