Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Check if checkbox is checked and alert "Hi"

So I have a dropdown list with checkboxes.

<div id = "dropdown" class ="dropdown">
   <div class = "dropdown-row">
     <label
       <span class="text-space>Item 1</span>
       <input type="checkbox" class="clickable">
     </label>
   </div>
      <div class = "dropdown-row">
     <label
       <span class="text-space>Item 2</span>
       <input type="checkbox" class="clickable">
     </label>
   </div>
   <div class = "dropdown-row">
     <label
       <span class="text-space>Item 3</span>
       <input type="checkbox" class="clickable">
     </label>
   </div>
   <div class = "dropdown-row">
     <label
       <span class="text-space>Item 4</span>
       <input type="checkbox" class="clickable">
     </label>
   </div>
   <div class = "dropdown-row">
     <label
       <span class="text-space>Item 5</span>
       <input type="checkbox" class="clickable">
     </label>
   </div>
   <div class = "dropdown-row">
     <label
       <span class="text-space>Item 6</span>
       <input type="checkbox" class="clickable">
     </label>
   </div>
</div>

I am trying to check if checkbox is checked and just simple text "Hi".
But it doesn’t work. I am not sure but I am trying to get all inputs with type="checkbox" and for each checkbox addEventListener on click.

const checkboxes = document.querySelectorAll('input[type="checkbox"]')
for (let i = 0; i < checkboxes.length; i++) {
    checkboxes[i].addEventListener("click", function() {
        if (checkboxes[i].checked) {
            alert("Hi")
        }
    })
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Add an event listener for the change event. It triggers whenever a checkbox is checked and unchecked. Use this.checked to verify if the checkbox is checked or not. Note that it is safe to use this keyword here because the function in addEventListener is a regular function and not an arrow function.

const checkboxes = document.querySelectorAll('input[type="checkbox"]');

for (let i = 0; i < checkboxes.length; i++) {
    checkboxes[i].addEventListener("change", function() {
        if (this.checked) {
            console.log(`Hi from Checkbox #${i + 1}`);
        }
    })
}
<div id="dropdown" class="dropdown">
   <div class="dropdown-row">
     <label>
       <span class="text-space">Item 1</span>
       <input type="checkbox" class="clickable">
     </label>
   </div>
      <div class = "dropdown-row">
     <label>
       <span class="text-space">Item 2</span>
       <input type="checkbox" class="clickable">
     </label>
   </div>
   <div class = "dropdown-row">
     <label>
       <span class="text-space">Item 3</span>
       <input type="checkbox" class="clickable">
     </label>
   </div>
   <div class = "dropdown-row">
     <label>
       <span class="text-space">Item 4</span>
       <input type="checkbox" class="clickable">
     </label>
   </div>
   <div class = "dropdown-row">
     <label>
       <span class="text-space">Item 5</span>
       <input type="checkbox" class="clickable">
     </label>
   </div>
   <div class = "dropdown-row">
     <label>
       <span class="text-space">Item 6</span>
       <input type="checkbox" class="clickable">
     </label>
   </div>
</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading