Count options disabled from a select with jquery

Asked

Viewed 83 times

2

Boas, how can I count options with the disabled option selected and efficiently with jquery?

2 answers

1


You don’t need jQuery for that. You can only 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>
    <option value="Apple">Apple</option>
    <option value="Banana" disabled>Banana</option>
    <option value="Orange" disabled>Orange</option>
    <option value="Grape">Grape</option>
</select>

  • thanks, for the answer! With jquery decreases the size of the code making it more efficient.

  • @Musiclyricshq if your page does everything without jQuery then yes it is more efficient because jQuery loads many lines of code to do simple things like this.

  • my page has several uses of jquery

  • At first you say that "Não precisas de jQuery para isso." and then the answer is with Jquery!! Grounded, I will not give you link Sun January - Live in South Korea :(

  • @Leocaracciolo haha :) I said it was better without, but the request in the question was with, so I gave both.

  • +1 and go there, https://www.youtube.com/watch?v=Rr7TaaJ60Ws

Show 1 more comment

0

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>
    <option value="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>

  • very good, straight and straight, +1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.