Blankman
Blankman

Reputation: 266978

How to tell if a drop down has options to select?

How to tell if a drop down has options to select?

Upvotes: 13

Views: 17191

Answers (7)

Neil
Neil

Reputation: 55392

Very hackily, you can just check its selectedIndex; since most browsers ensure that there is something selected if at all possible, it will only be -1 if there are no selectable options.

Upvotes: 0

jessegavin
jessegavin

Reputation: 75650

if ($("#myselect option").length > 0) {
  // Yay we have options
}

Upvotes: 10

Phrogz
Phrogz

Reputation: 303206

if ($("#myselect option:enabled").length){
   // Yay!
}else{
   // Oh, no available options
}

http://api.jquery.com/enabled-selector/

Upvotes: 2

Pablo Fernandez
Pablo Fernandez

Reputation: 105220

Native javascript solution:

!!document.getElementById('jiveviewthreadsform-filter').children.length

(Please do not overuse jQuery, thanks)

Upvotes: 0

Scott
Scott

Reputation: 13921

$('#input1 option').length > 0

Where #input is the ID of the select element you are running this test against.

Upvotes: 3

Makram Saleh
Makram Saleh

Reputation: 8701

var menu = getElementById("select_id");
if(menu.options.length) {
    // has children
} else {
    // empty
}

Upvotes: 15

Pointy
Pointy

Reputation: 413712

var hasOptions = !!$('#theSelect option').filter(function() { return !this.disabled; }).length;

maybe? This looks for <option> elements that are not disabled.

Upvotes: 8

Related Questions