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

JavaScript check and uncheck checkbox, after uncheck checkbox return to the original state

I’m using JavaScript to code a function, that if checkbox is checked disabled the select dropdown, then if uncheck the checkbox and enable the select dropdown.

HTML:

 <input type="checkbox" id="flag">

  <select name="cars" id="select" disabled>
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>
  </select>

This is how I do it:

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

 function summary()
  {
    if (document.getElementById('flag').checked) 
    {
      document.getElementById('select').disabled = true;
    }
    else{
    document.getElementById('select').disabled = false;
    }
  }

My question is, instead of repeating the duplicated code document.getElementById('select').disabled =, is there a shorter way to achieve this logic?

>Solution :

Sure, you could shorten your JS like this by setting one boolean to another.

function summary() {
  document.getElementById('select').disabled = document.getElementById('flag').checked;
}
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