lundi 20 avril 2020

Changing CSS values with checkbox and javascript

I am trying to hide and display a div based on the state of a checkbox using javascript but I don't seem to be getting it right. I am not very experienced with js so I could be missing something very obvious. Any advice would be much appreciated.

var checkbox = document.querySelector("input[name=toggle]");

checkbox.addEventListener( 'change', function() {
    if(this.checked) {
        document.getElementById("on-toggle").style.display = "block";
        document.getElementById("on-toggle").style.height = "auto";
    } else {
        document.getElementById("on-toggle").style.display = "none";
        document.getElementById("on-toggle").style.height = "0";
    }
<div id="switch">
                <h2 id="CTA-switch">Turn creativity on </h2>
                <div class="switch">
                        <input type="checkbox" name="toggle">
                        <label for="toggle">
                    <i class="bulb">
                      <span class="bulb-center"></span>
                      <span class="filament-1"></span>
                      <span class="filament-2"></span>
                      <span class="reflections">
                        <span></span>
                      </span>
                      <span class="sparks">
                        <i class="spark1"></i>
                        <i class="spark2"></i>
                        <i class="spark3"></i>
                        <i class="spark4"></i>
                      </span>
                    </i>    
                  </label>
                </div>
            </div>



Aucun commentaire:

Enregistrer un commentaire