Log in

View Full Version : List Box Count with a button



alextheveedubguy
11-12-2009, 12:46 PM
Hey everyone,

I'm looking for a way to count the list box items once the user has click them and with a button display how many that the user selected.

jscheuer1
11-12-2009, 03:56 PM
There is no list box. I think you are referring to a select element with the multiple attribute, but I cannot be sure. If so, this will work:


<script type="text/javascript">
function howMany(el){
for (var s = 0, i = el.options.length - 1; i > -1; --i)
if(el.options[i].selected) ++s;
return s;
}
</script>
<select id="mySel" multiple name="" onchange="alert(howMany(this));">
<option value="">1</option>
<option value="">2</option>
<option value="">3</option>
<option value="">4</option>
<option value="">5</option>
</select><br>
<input type="button" onclick="alert(howMany(document.getElementById('mySel')));" value="How Many">


If you want more help, please show us the markup and code you are working with, or:

Please post a link to the page on your site that contains the problematic code so we can check it out.