Rob Campbell
Rob Campbell

Reputation: 63

HTML checkbox to Javascript form

I am (still) working on creating sidebar input for a Sheets add-on I'm developing. My current challenge is allowing a textbox and a checkbox to be passed from the form to my function.

The textbox works and successfully passes the variable. The checkbox I'm not as clear on. I need to turn the checkbox into a boolean.

The relevant html:

<p>
  <input type="text" id="datepicker">
  </p>
  <p>
  Include pledge?<br>
    <input type="checkbox" name="pledge[]" id="pledgeCheck" value="true">Yes<br>
  </p>
          <p>
            <input type="button" name="createdate" value="Create" onClick="google.script.run.create(document.getElementById('datepicker').value,document.getElementById('pledgeCheck').value)" />
          </p>

function create(datepicker,pledge) {

  //important vars
  var responses = ss.getSheetByName("Form Responses");
  var bulletin = ss.getSheetByName(todayDate);
  var todayDate=datepicker;
  var pledgeCheck=pledge;
  Logger.log(pledgeCheck);
  

currently the pledge feeds back as undefined.

SOLVED: A few things I wasn't getting, and maybe a few I just sort of overlooked and missed. I didn't have the right terms in all the right places. @umair pointed the way in my html, as shown below. My js, however, used pledge instead of pledgeCheck in the arguments.

Upvotes: 0

Views: 87

Answers (1)

Umair Mohammad
Umair Mohammad

Reputation: 4635

  1. Remove the value from checkbox, otherwise it will be always true.

  2. Check whether the checkbox is checked or not with .checked attribute and not with .value; this will return true for checked and false for unchecked.

<p>
   <input type="text" id="datepicker">
</p>
<p>
   Include pledge?<br>
   <input type="checkbox" name="pledge" id="pledgeCheck">Yes<br>
</p>
<p>
   <input type="button" name="createdate" value="Create" onClick="google.script.run.create(document.getElementById('datepicker').value,document.getElementById('pledgeCheck').checked)" />
</p>

Upvotes: 1

Related Questions