Check if checkbox is checked, if not check it - Javascript

I want to use javascript to check if the Checkbox field is checked. If it is not checked, then the script has to check it. I also would like to create an inverse function that will uncheck the Checkbox, when it is checked. Can you give me advice how to do it ?
give a positive ratinggive a negative rating
01 Jun 2019 at 02:31 PM

This solution explains how the checking works on event:

<input type="checkbox" id="a1" />

<a href="javascript:void(0);" onClick="javascript: if(document.getElementById('a1').checked==true){ document.getElementById('a1').checked=false; } else{ document.getElementById('a1').checked=true; } " >Check</a>

To check and uncheck the checkbox on event, you can also use this solution:

<input type="checkbox" id="b1" />

<a href="javascript:void(0);" onClick="javascript: document.getElementById('b1').checked= !document.getElementById('b1').checked; " >Check</a>
give a positive ratinggive a negative rating
05 Jun 2019 at 03:12 PM
Share on FacebookShare on TwitterShare on LinkedInSend email
2024 AnswerTabsTermsContact us