vendredi 5 juin 2020

How to check/uncheck a checkbox in pure JS using an onclick event?

Here is my code

<div>
   <p onclick="toggleOffer()">1 mois</p>
   <input type="checkbox" id="select-time" checked>
   <p onclick="toggleOffer()">12 mois</p>
</div>
<script>

    function toggleOffer() {
        let toggleButton = document.getElementById("select-time");
        if (toggleButton.checked = true){
            toggleButton.checked= false;
        }else{
            toggleButton.checked= true;
        }
    }
</script>

It works one way : when my checkbox is checked and I click on either <p>, the checkbox gets unchecked, but I can't get the other way round to work.




Aucun commentaire:

Enregistrer un commentaire