I have a list of dynamically created checkboxes named "tags" which I loop through with javascript to make sure only 1 is checked and to get the value. If for some reason this list of checkboxes only has 1 item, my script won't look at it. When I alert the tag's length I get undefined.
var total=[];
for (var i=0; i < document.form1.tags.length; i++){
if(document.form1.tags[i].checked==true){
total.push(document.form1.tags[i].value);
}
}
if(total.length==0){
alert("Please select a product to edit.");
}else if (total.length>1){
alert("Please select only one product to edit.");
}else{
document.location = "go somewhere";
}
.tagsis not an array when there is only one checkbox, it contains direct reference to the one checkbox. It only becomes a nodelist when there are more than one checkboxes with the same name. You should always look at the developer console and you would have seen the error there.querySelectorAllor jQuery.