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 ?
01 Jun 2019 at 02:31 PM
0pnshow more
onefiveonefive
Hi,

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>
05 Jun 2019 at 03:12 PM
1pnshow more
TimTim
Share on FacebookShare on TwitterShare on LinkedInSend email
Follow us on Facebook & Twitter
2022 AnswerTabsTermsContact us