John Smith
John Smith

Reputation: 1687

Checking if a selectbox is empty

How do you check if a selectbox is empty with plain JavaScript and alert the user "The selectbox contains 0 items."

Upvotes: 2

Views: 17085

Answers (2)

Musa
Musa

Reputation: 97672

if (document.getElementById('select').options.length == 0) 
    alert('The selectbox contains 0 items');

http://jsfiddle.net/Gf8QK/

Upvotes: 6

dfmiller
dfmiller

Reputation: 1203

Use:

if (selectObject.options.length == 0) {
    alert('The selectbox contains 0 items');
}

See http://www.w3schools.com/jsref/coll_select_options.asp

Upvotes: 0

Related Questions