I have a "sign up for email updates" checkbox that is checked by default with a value of "Yes". If unchecked, it needs to be set to "No". This is not a perfect solution, but these are the values that need to be submitted to the database on form submission.
The HTML:
<input name="emailSignUpCheckbox" id="emailSignUpCheckbox" type="checkbox" value="Yes" onclick="updateSignUpCheckbox()">Sign me up!
The Javascript:
var emailSignUpCheckbox = document.getElementById('emailSignUpCheckbox');
function updateSignUpCheckbox(){
if(emailSignUpCheckbox.checked) {
emailSignUpCheckbox.value = "Yes";
}
else {
emailSignUpCheckbox.value = "No";
}
}
The value of the checkbox is being changed each time and I have confirmed it by looking at the DOM or by logging the value in the console. And when submitted as checked (default), the value of "Yes", is posted by the form to the database.
However, if unchecked manually, the value of the checkbox is not posted by the form to the database, but a blank value is instead posted.
Any suggestions on what I could be doing wrong are greatly appreciated!