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
  • 661
  • 1
  • 6
  • 15

3 Answers3

2
if ( $("#select_id option").length == 0 ) {
    function()
}
Galen
  • 29,682
  • 9
  • 70
  • 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
  • 222,134
  • 40
  • 319
  • 330
0
$("select").each(function(){
    if(this.childNodes.length > 0)
        process();
});
Joseph Marikle
  • 72,900
  • 16
  • 109
  • 126