Sha
Sha

Reputation: 23

Make a unchecked HTML checkbox submit a checked value

I am using a Zoho CRM web to lead form and I need to include an Email Opt Out.

By using the following field, if the checkbox is checked, the user is unsubscribed in the Zoho CRM.

<input type="checkbox"  name="Email Opt Out" />

However, I need to reverse this, so that the field to unsubscribe the user if the checkbox is unchecked, but not submit anything/unsubscribe the user is the box is checked.

Essentially I need the checkbox to work the other way round: a checked value is submitted if the checkbox is unchecked, and and unchecked value submitted if it is checked.

I have tried the following, but it doesnt work: ** Note - I would like the checkbox to be checked by default.

 <input type="hidden" name="Email Opt Out" value="TRUE"/>
 <input type="checkbox" name="Email Opt Out" checked="checked" value="FALSE"/>

Upvotes: 2

Views: 788

Answers (1)

Jamie Barker
Jamie Barker

Reputation: 8246

Invert the value with jQuery/JS:

https://jsfiddle.net/09noubr9/

$('input[name="boolValue"]').on('change', function() {
  $('input[name="invertBoolValue"]').val(!$(this).is(':checked'))
});
<input type="checkbox" name="boolValue" />
<input type="hidden" name="invertBoolValue" />

Upvotes: 1

Related Questions