Reputation: 34170
How to check whether multiple select box contains any element or not.In this case there are no elements in the multiselect box.I tried the following.I get an error at selected_available_segments.length
as null
<select multiple id="selected_available_segments">
</select>
Jquery
var selected_available_segments = $("#selected_available_segments").val();
if ($('#selected_available_segments').size() != 0) {
for(var i =0; i< selected_available_segments.length;i++)
{
alert("Got it");
}
}
Upvotes: 0
Views: 432
Reputation: 4110
if ($('#selected_available_segments').is(':empty') === false) {
//
}
or
if (!$('#selected_available_segments').is(':empty')) {
//
}
(However, i find it's easy to miss the !
here, so I prefer my first solution.)
JSFiddle: http://jsfiddle.net/KARN2/1/
Upvotes: 0
Reputation: 10994
var options = $('#selected_available_segments option').length;
if (options){
for(var i = 0; i < options; i++)
{
alert("Got it");
}
}
Upvotes: 0
Reputation: 104775
Check the option
size of the list:
if ($("#selected_available_segments option").length) {
console.log("Im there!");
};
Demo: http://jsfiddle.net/Tp4EB/
Upvotes: 2