I need to clear total number of devices input field once clicked on checkbox:
If I click on the checkbox Android a number of devices input field pops up then I select the amount of devices and total number of devices input automatically fills in the number, I would like to clear that number when unchecking the Android checkbox: sample of form here:
https://mobile-guardian.webflow.io/form-template
This is the what the script is at the moment:
<script>
document.addEventListener("DOMContentLoaded", function() {
const deviceInputs = document.querySelectorAll('#chromebook-device, #windows-device, #macos-device, #android-device, #ios-device');
const totalInput = document.getElementById('total-device');
deviceInputs.forEach(input => {
input.addEventListener('input', function() {
let totalDevices = 0;
deviceInputs.forEach(input => {
const number = parseInt(input.value, 10) || 0;
totalDevices += number;
});
totalInput.value = totalDevices;
});
});
});
</script>
3
Answers
If you go document.getElemementById(‘your boxes class here’).innerhtml(”) then I think it should clear it
I think this will be solve your problem.
Declare your process as function
add event listener for checkbox
If you want to empty totalInput as you said just try
I create array of objects to set dependances idChBox and idTextInput, modify getters of nodeList and create function to get actual nodeList of textInputs. If desired, the code can be shortened.
To make the code shorter, I recommend giving classes for checkboxes and text fields (general, typed and clarifying).
Example chbox: os-class os-text android (mac , ios)
Example text: os-class os-chbx android (mac , ios)
Then you don’t need to create devicesList and use classes instead.