Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery selectors: multiselect vs select

Is it possible, via jQuery, to select a multiselect to a select, differently?

The issue I'm having is that I have a general js called that does something to an id. However on some pages it has a different purpose.

Therefore I would like about to recognize the difference.

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

Possible?

like image 325
azz0r Avatar asked Sep 06 '10 16:09

azz0r


1 Answers

The correct attribute name for a <select> element with multiple selectable options is multiple. You can use the "has attribute" selector to select elements with the multiple attribute, and combine it with :not() to select elements that only allow a single 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

like image 94
Andy E Avatar answered Sep 24 '22 21:09

Andy E