I have a question as to how can I calculate number of options tag when I have multiple select box with same class and id?
Let's say I have three select boxes. And I want the size of select box, so that I can dynamically add new select box with the same options:
<select id="selectid" class="selectclass">
<option>1</option>
<option>2</option>
</select>
<select id="selectid" class="selectclass">
<option>1</option>
<option>2</option>
</select>
<select id="selectid" class="selectclass">
<option>1</option>
<option>2</option>
</select>
You will use the following command to determine if there are any options within the select box. var length = $('#selectBoxId > option'). length; console.
var length = $('#mySelectList > option'). length; This assumes your <select> list has an ID of mySelectList .
querySelectorAll() method to the number of drop-down options. Since the options are created using the <options> tag within the document, you can use the . querySelectorAll() method to access the length as well as the value.
with jquery:
for a given select
with an id
:
$('select#selectid option').length
for all selects
with a given class:
$('select.selectclass option').length
for all selects
in the page:
$('select option').length
but you should give different Ids to each element in a html page
Tag IDs are supposed to be unique to a document. Do you plan to have all of these in the document tree at the same time?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With