In my page I have an array of string and in my script I have a function that excludes the strings that contain certain char.
I control it through 3 check boxes.
I have the functions to do this but my problem is when I check two check boxes one to exclude the strings containing 'a' and one containing 'e'. I get the result of the last checked check box.
How to make a function that will handle all the check boxes and show me the final result.
This is what I have so far :
My html :
<div id="demo"> </div>
<div class="item">
<form id="aForm" onchange="filter()">
<input type="checkbox" id ="A" value="A">Exclude words with 'A'<br>
<input type="checkbox" id ="E" value="E">Exclude words with 'E'<br>
<input type="checkbox" id ="O" value="O">Exclude words with 'O'<br>
<form id="aForm" >
</div>
<script type="text/javascript">
var animals = ["Bear", "Mouse", "Cat", "Tiger", "Lion"]
function filter () {
if(document.getElementById('A').checked) {
var result2 = [];
for (var animal in Animals) {
if (Animals[animal].indexOf('a') == -1) {
result2 += " " + Animals[animal];
}
document.getElementById("demo").innerHTML = result2;
}
}
if(document.getElementById('E').checked) {
var result2 = [];
for (var animal in Animals) {
if (Animals[animal].indexOf('e') == -1) {
result2 += " " + Animals[animal];
}
document.getElementById("demo").innerHTML = result2;
}
}
if(document.getElementById('O').checked) {
var result2 = [];
for (var animal in Animals) {
if (Animals[animal].indexOf('o') == -1) {
result2 += " " + Animals[animal];
}
document.getElementById("demo").innerHTML = result2;
}
}
}