Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript - get length of list options

Is there a way I can get the length of list options in a select-dropdown HTML input tag? The list changes dynamically and I need to calculate the total number of options within the drop-down. The total options are generated dynamically, so I need a way to calculate number of option tags within an html select tag. I also need to do this in pure JS because the app I am working with will not allow me to use JQuery. (Please don't argue that with me unless there is absolutely no way to do this in pure JS.)

Needs only to be compatible with Internet Explorer.

I assume this will have to do with accessing the DOM, but I am not sure how exactly the syntax will work out.

like image 362
eatonphil Avatar asked Oct 31 '12 19:10

eatonphil


People also ask

How to get the length of dropdownlist in JavaScript?

To get the count of options in a dropdown list, use the length property in JavaScript.

How do I get the number of options in select?

You will use the following command to determine if there are any options within the select box. var length = $('#selectBoxId > option'). length; console. log(length);

What is option length?

options. length is "the number of elements in the list of options childNodes on the select element". Technical difference, semantically slightly different, but due to how select. length has been formalised, for all intents and purposes always the same value.


4 Answers

Since you didn't specify that you could use jQuery, try this:

HTML:

<select id="test">
  <option>1</option>
  <option>2</option>
  <option>3</option>
  <option>4</option>
</select>​

JavaScript:

​document.getElementById("test").options.length

example

like image 183
Chase Avatar answered Oct 02 '22 05:10

Chase


Select the options from the select input, then count them with length

$("#input1 option").length
like image 22
Pastor Bones Avatar answered Oct 02 '22 06:10

Pastor Bones


i did this Using JQuery

$(document).ready(function() {
    $("button").click(function(){
      var optionLength = $("#user option").length;
      
           alert("length of options is : "+optionLength);
    });
});
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Get length of list Options</title>
</head>
  <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>

<body>
<select id="user" multiple="" size="5" class="form-control" id="participants" name="participants" style="height: 98px !important;">
  <option value="1">S RAJ</option>
  <option value="2"> SHARMA</option>
  <option value="3">SINGH</option>
  <option value="4"> ROY</option>
  <option value="5">VERMA</option>
  <select>
    <button type="button">Get Users</button>
</body>
</html>
like image 44
ssjvirtually Avatar answered Oct 02 '22 06:10

ssjvirtually


Try this:

In the HTML file:

<select id="mySelect">
  <option>Apple</option>
  <option>Pear</option>
  <option>Banana</option>
  <option>Orange</option>
</select>

In the JavaScript file:

document.getElementById("mySelect").length;
like image 45
Grzegorz Avatar answered Oct 02 '22 04:10

Grzegorz