I have a multiple selection list which have more than 5 options, and i want to count the number of selection of the options selected by the user.
How to do it using java script?
I tried the following but it didn't work for me:
var x = document.getElementById("preference").count;
Thanks in Advance.
Use the :selected
selector, like this:
$('#example option:selected').length;
Assume foo
is the id of the select.
If you use normal javasript:
var options = document.getElementById('foo').options, count = 0;
for (var i=0; i < options.length; i++) {
if (options[i].selected) count++;
}
If you use jQuery:
var count = $('#foo option:selected').length;
You can try this to get multiple select boxes user selected options count and their selected names. Try this link http://jsfiddle.net/N6YK8/8/
function getCount(){
var comboBoxes = document.querySelectorAll("select");
var selected = [];
for(var i=0,len=comboBoxes.length;i<len;i++){
var combo = comboBoxes[i];
var options = combo.children;
for(var j=0,length=options.length;j<length;j++){
var option = options[j];
if(option.selected){
selected.push(option.text);
}
}
}
alert("Selected Options '" + selected + "' Total Count "+ selected.length);
}
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