Tell the disabled options of a select with jquery

2

How can I count the options with the disabled option selected and efficiently with jquery?

    
asked by anonymous 28.10.2017 / 18:27

2 answers

2

You do not need jQuery for this. You can do it with JavaScript and CSS selector :disabled

const nrA = document.querySelectorAll('option:disabled').length;
console.log('A', nrA);

const nrB = $('option:disabled').length;
console.log('B', nrB);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><select><optionvalue="Apple">Apple</option>
    <option value="Banana" disabled>Banana</option>
    <option value="Orange" disabled>Orange</option>
    <option value="Grape">Grape</option>
</select>
    
28.10.2017 / 18:35
1

A simple way to count using jQuery:

conta = $('select option:disabled').length;
console.log(conta);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><select><optionvalue="1" disabled="disabled">A</option>
    <option value="2" disabled="disabled">B</option>
    <option value="3">C</option>
    <option value="4" disabled="disabled">D</option>
</select>
    
28.10.2017 / 18:45