How can I check if my <select> element contains multiple attributes

Hi, I tried many options to check if the multiple attribute is set in my select box, but no one worked. I am trying to determine if the current selection block, which I am getting the values ​​from from several selected so far, is what I tried:

if($(":select[multiple]").length){
           alert("worked");
}

and

if($("select").attr("multiple"){
           alert("worked");
}

and

if($("select").attr("multiple") != 'undefined'{
           alert("worked");
}

HTML:

<select multiple="multiple" style="height:50px" class="classname" name="multi_values[]"> 
 <option value="blah">blah</option> 
 <option value="blah">blah</option> 
 <option value="blah">blah</option>              
</select>
+5
source share
4 answers

delete :at the beginning:

if($("select[multiple]").length){
    alert("worked");
}

Demo : http://jsfiddle.net/D5JX5/

+8
source

Also a simple javascript check:

var c = document.getElementsByTagName('select'); //collection
for (var i=0, l = c.length; i<l; i++) {
    alert(typeof c[i].attributes['multiple'] == 'undefined' ? 'single':'multiple');
}

And the jQuery equivalent:

$('select').each(function(){
  alert( typeof this.attributes['multiple'] == 'undefined' ? 'single':'multiple' );
});
+2
source

, ":select[multiple]" (shd be "select[multiple]"), , .

JSFiddle: http://jsfiddle.net/VAXF6/2/

if.

:

if($("select[multiple]").length){
           alert("worked");
}

if($("select").attr("multiple")){
           alert("worked");
}

if($("select").attr("multiple") != 'undefined'){
           alert("worked");
}

:

if($("select").is("[multiple]")){
           alert("worked");
}
+1

It seems you only need to notify if several values ​​with a value have been set, and not only if it exists as an attribute:

if($("select[multiple='multiple']").length){
    alert("worked"); 
}
+1
source

All Articles