I have some input boxes that I want to disable for a few seconds if one is clicked.
I am using setTimeout and disabled property to make the check boxes un-clickable. Then I would like to use clearTimeout to make it clickable again after moments have passed.
Two issues I'm running into:
- If I click one box, it only disables the first checkbox. I'm assuming it's because it only disables the first checkbox with the
id='test'But if I change it togetElementsByClassNamethedisableddoesn't work. - The
clearTimeoutis just not working.
Here is a jsfiddle. You can comment out lines 6-8 in the Javascript section to see how the disableClick() function works with an ID, but only disables Digital Filter.
Here is my HTML:
<input id="test" type="checkbox" value="digital-filter" onclick="setTimeout(disableClick, 500)">
<span>Digital Filter</span>
<input id="test" type="checkbox" value="meter" onclick="setTimeout(disableClick, 500)">
<span>Meter</span>
Here is my javascript:
var subfilters;
function disableClick(){
subfilters = document.getElementById("test").disabled = true;
}
setTimeout(() => {
clearTimeout(subfilters);
}, 250);
You may try something simpler, without
clearTimeout.