views:

41

answers:

4

hi guys

How can i know is there any options in a combobox/selectbox or not?

small Edit:

i have my comboxbox as myCombo = $("#country");

now i want to know how many options are there in myCombo

A: 

You can use the length property like this:

alert($('#dropdown_id option').length);

Make sure to wrap your code in ready handler:

$(function(){
  alert($('#dropdown_id option').length);
});

You can also use the size() method:

$(function(){
  alert($('#dropdown_id option').size());
});
Sarfraz
You mean `#dropdown_id option`, I think -- `option` is a tag, not a pseudoselector.
lonesomeday
@lonesomeday: Exactly, updated thanks.
Sarfraz
+1  A: 

In jQuery:

if($('select#something option').length > 0) {
    // There are some.
    ...
Tatu Ulmanen
A: 
<select id="mySelect">
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
</select>

Then:

alert($('#mySelect > option').size()); //4
Ben
@Ben from [the API](http://api.jquery.com/size/): "You should use the .length property instead, which is slightly faster."
lonesomeday
@lonesomeday - Yeah, you're right.
Ben
+1  A: 

$(myCombo).children("option").length

Tom