Hi all,
Could anybody tell me how to count the number of options are selected in a select.
Than u all.
Hi all,
Could anybody tell me how to count the number of options are selected in a select.
Than u all.
var cnt = 0;
for (var i = 0; i < el.options.length; i++) {
if (el.options[i].selected) {
cnt++;
}
}
function createtext(field)
{
var noele = document.getElementsByName(‘matname’);
var cnt = 0;
for (var i=0;i<noele.options.length;i++){
if(noele[i].selected){
cnt++;
}
}
alert(cnt);
}
Could anybody tell where I am going wrong with the above code. I am not getting the output. I want to alert how to options are selected in a select.
The getElementsByName()
function returns a NodeList
, not a single Node
. So you need to use noele[0]
to reference your element.
A better way would be to specify an ID for the element and use getElementById()
, which returns a single node. You should need an ID anyway, in order to associate a <label>
with the <select>
.