Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 228
  • Last Modified:

Referecing the boolean value of a checkbox from a function

I am a javascript newbie. I am using Google Maps on my site, and add the following control:

<input type="checkbox" id="chkA" onClick="getOverlays()">

From my getOverlays function, I want to be able to use the IF statement (or case) to decide the incoming value from the checkbox, ie, if it is checked or not. That way I can run either of two scenarios.

How can I pass the checked value of the checkbox through to the function?

1 Solution
Hi Craig,

To assign the boolean value of the checkbox to the variable 'bool,' place the following line within your function getOverlays() :

var bool = document.getElementById('chkA').checked;

Peace and joy.  mvan
ZvonkoSystems architectCommented:
Or you extend your function call to this:
<input type="checkbox" id="chkA" onClick="getOverlays(this)">

Then your checkbox value acces can be done like this:


function getOverlays(theBox){
    // do this
  } else {
   // do that


Also, if you wanted to keep your function from having to deal with a checkbox object and just receive a boolean, you can pass directly from your input element:


function getOverlays(someBool)
    // Do whatever you want with the value stored in someBool


<input type="checkbox" id="chkA" onClick="getOverlays(this.checked)">

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now