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.
If < IE8 isn't a concern, you can do a one-liner like
document.querySelectorAll("option:checked")
to get all the selected options.Assume
foo
is the id of the select.If you use normal javasript:
If you use jQuery:
Use the
:selected
selector, like this: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/