I'm struggling to find the error within my code. My function is triggered whenever an option is changed. I first cycle through all the selects to see which values have been selected. I then cycle through them again and then their children to HIDE all the options that have been taken. I am sure that there are other posts on stack overflow that have better ways of doing this, but I would rather have my original code. I can't find my error, though. I have put alerts everywhere but they are always called only once, even though the .length
is > 1.
Thanks!
EDIT:
- I just realized the problem was with the
.removeAttr
so if anyone could tell me how to fix that, that might be just the help I need.
$(document).on("change", function(e) {
var hmmm = [];
var facts = true;
var allSelects = $(".student-three select");
for (var i = 0; i < allSelects.length; i++) {
if (allSelects[i].value != 0) {
hmmm.push(allSelects[i].value);
}
}
for (var i = 0; i < allSelects.length; i++) {
for (var j = 0; j < allSelects[i].children.length; j++) {
for (var z = 0; z < hmmm.length; z++) {
if (allSelects[i].children[j].value == hmmm[z]) {
allSelects[i].children[j].addAttr('hidden');
facts = false;
}
}
if (facts) {
allSelects[i].children[j].removeAttr('hidden');
}
}
}
});
<!DOCTYPE>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<div class="student-three">
Choice 1:
<select name="C1">
<option value='0'>Select One</option>
<option value='1'>Option 1
</option>
<option value='2'>Option 2
</option>
</select><br>Choice 2:
<select name="C2">
<option value='0'>Select One</option>
<option value='1'>Option 1
</option>
<option value='2'>Option 2
</option>
</select><br>Choice 3:
<select name="C3">
<option value='0'>Select One</option>
<option value='1'>Option 1
</option>
<option value='2'>Option 2
</option>
</select>
</div>
</body>
</html>