[ACCEPTED]-How to check with jquery if select has options?-html

Accepted answer
Score: 41

One way to do to it:

if ($('#myselect option').length == 0) {
    $('#myselect').hide();
}

0

Score: 7

var $select = $('input:select option');
if(!$select.length )
    $select.attr('disabled', true);

Or:

$('select:not(:has(option))').attr('disabled', true);

0

Score: 6

Another way to do it is simply use the jQuery 2 has function

if ($('#myselect').has('option').length == 0) 
{
    // Hide and disable select input here
}

For information on the jQuery has function 1 see: https://api.jquery.com/has/

Score: 3
select:not(:has(option))

is the selector you'd be looking for. You 4 can use it to find the empty select boxes, disable 3 them, and then append an option saying "No 2 option available" or some such message 1 - like so:

var noOptions = $('<option/>').html('No options');
$('select:not(:has(option))').attr('disabled',true).append(noOptions);

Live example here: http://jsfiddle.net/f3myz/

Score: 1

if you have the empty option, then you can:

#Allways you must have the empty option
if ($("select#your_class").children().size() == 1) {
  alert("Without options")
  $("select#your_class").hide()
} else {
  alert("With options")
  $("select#your_class").show()

0

Score: 0

let baseCategory = $('#base-categories');

if (baseCategory.children().length > 0) {
  let optionCount = baseCategory.children().length;
  console.log("select has " + optionCount + " option ")
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="base-categories">
  <option value="0">option 1</option>
</select>

0

More Related questions