Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determine total number of selected items in a select box

Status
Not open for further replies.

WildWest

Programmer
Joined
Apr 2, 2002
Messages
111
Location
US
Using JavaScript, does anyone know hoe to determine the total number of selected items in a select box and display it?
 
Here's a simple script to find the total number of highlighted elements in the dropdown.
Press ctrl to select multiple items
Code:
<html>
<head>
<script type="text/javascript">
function countOptions(obj) {
   var len = obj.length;
   var cnt = 0;
   for (i = 0; i < len; i++) {
      cnt = (obj.options[i].selected) ? cnt + 1 : cnt;
   }
   alert("There are " + cnt + " items selected in the dropdown");
}
</script>

</head>
<body>
<form name=blahForm>
<select name=blahSelect multiple>
<option value=0>zero
<option value=1>one
<option value=2>two
<option value=3>three
<option value=4>four
<option value=5>five
<option value=6>six
<option value=7>seven
<option value=8>eight
<option value=9>nine
</select>
<input type=button value='Click Me' onclick='countOptions(blahForm.blahSelect)'>
</form>
</body>
</html>

-kaht

banghead.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top