1

this might be related to this question

I have a dropdown menu that outputs a option list. In some cases the backend script outputs no options for the select tag.

I want to run a js/jquery that checks for the existence of option tags. If option tag available do nothing If no option tag available run function X.

Cheers

Community
  • 1
  • 1
Christian
  • 681
  • 1
  • 6
  • 15

3 Answers3

2
if ( $("#select_id option").length == 0 ) {
    function()
}
Galen
  • 29,976
  • 9
  • 71
  • 89
  • I tried this:function hideThebutton() { $("add-to-cart").hide(); }; if ($("#size-attribute option").length == 0 ) { hideThebutton(); }; but it does not work any idea why, I want to run this on page load obviously – Christian Sep 14 '11 at 12:19
1

You can use length to count the number of elements matched by a selector. In this case, write a selector that matches any of the <option> tags inside your select:

if ($('select.my_select option').length > 0) {
  // There are options inside <select class="my_select">
}
user229044
  • 232,980
  • 40
  • 330
  • 338
0
$("select").each(function(){
    if(this.childNodes.length > 0)
        process();
});
Joseph Marikle
  • 76,418
  • 17
  • 112
  • 129