I am printing list of arrays using checkboxes, the scenario is if I click one checkbox it display a list of numbers in the array, so my problem is if I click any checkbox it is displaying if I uncheck list is still in the display? so I want the code for, if I uncheck my list should go and when I click it should display.
var men=[1,2,5,4,8,1,5];
var women=[45,55,45];
var children=[256,365];
document.getElementById("checkbox1").onchange=function(){
for(i=0; i<men.length; i++){
document.getElementById("userlist").innerHTML+= "<li>" + men[i] + "</li>";
document.getElementById("checkbox1").onchange='';
}
}
document.getElementById("checkbox2").onchange=function(){
for(i=0; i<women.length; i++){
document.getElementById("userlist").innerHTML+= "<li>" + women[i] + "</li>";
document.getElementById("checkbox2").onchange='';
}
}
document.getElementById("checkbox3").onchange=function(){
document.getElementById("checkbox3").onchange='';
for(i=0; i<children.length; i++){
document.getElementById("userlist").innerHTML+= "<li>" + children[i] + "</li>";
}
}
<label for="checkbox1">men</label>
<input type="checkbox" id="checkbox1">
<label for="checkbox2">women</label>
<input type="checkbox" id="checkbox2">
<label for="checkbox3">children</label>
<input type="checkbox" id ="checkbox3">
<ul id="userlist">
</ul>