I’m trying since days and I just cant get this to work. Maybe someone can help 🙂
The idea is, that when user clicks on specific checkbox, a specific div appears.
Because there is no regularity between inputs and display, sometimes more input fields, sometimes two difs to display, I thought it’s maybe better to assign each manually. This was my "Vacation"-Project and i hope i can get this to work… till work starts 🙂
Also I’d like this to function with other inputs : textarea, radio, select and input.
The only difference between checkbox element is value="checkbox1"
full element is: <input type="checkbox" name="versuch1" required="" **value="checkbox1"**>
the divs look like this and I can assign id to it:
`<div class="elementor-column elementor-col-33 elementor-top-column elementor-element elementor-element-71c1c46" data-id="71c1c46" data-element_type="column" **id="exampleoclumn1"**>
what I have:
`
</script>
// Element id
var checkbox1 = document.getElementTagName("checkbox1");
var checkbox2 = document.getElementTagName("checkbox2");
var checkbox3 = document.getElementTagName("checkbox3");
// Column id
var examplecolumn1 = document.getElementsById("examplecolumn1");
var examplecolumn2 = document.getElementById("examplecolumn2");
var examplecolumn3 = document.getElementById("examplecolumn3");
function updateColumnsDisplay() {
examplecolumn1.style.display = checkbox1.checked ? "block" : "none";
examplecolumn2.style.display = checkbox2.checked ? "block" : "none";
examplecolumn3.style.display = checkbox3.checked ? "block" : "none";
}
// event listeners to checkboxes
checkbox1.addEventListener("change", updateColumnsDisplay);
checkbox2.addEventListener("change", updateColumnsDisplay);
checkbox3.addEventListener("change", updateColumnsDisplay);
`
This was one of the previous attemps:
`
</script>
//element id
var checkbox1 = document.getElementById("checkbox1");
var checkbox2 = document.getElementById("checkbox2");
var checkbox3 = document.getElementById("checkbox3");
//column id
var examplecolumn1 = document.getElementById("examplecolumn1");
var examplecolumn2 = document.getElementById("examplecolumn2");
var examplecolumn3 = document.getElementById("examplecolumn3");
examplecolumn1.addEventListener("change", () => {
if(checkbox1.checked){
example-column1.style.display = "block";
if(checkbox2.checked){
example-column2.style.display = "block";
if(checkbox3.checked){
example-column3.style.display = "block";
}else{
example-column1.style.display = "none";
example-column2.style.display = "none";
example-column3.style.display = "none";
}
})`
2
Answers
For my answer, instead of using IDs I use data attributes. They are easier to use and you can reuse the same one.
For each checkbox, I add a data-target data attribute. This will hold a reference to the appropriate content divs.
I also created divs that have the class of content and a data attribute matching a checkbox. The content class is set to display none via CSS
I loop through these checkboxes and give each one an event handler for the change event.
Then in the call back function I get the changed checkbox via event.target. Then I use that in a querySelectorAll to target any divs with the class content and the matching data group attribute.
I then loop through the matching divs and toggle a CSS class called active. The active class has a display of none.
Just attach a listener to all inputs and the change event will tell you which one fired. Textarea and text inputs will fire
onKeyUp
event.