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?

Craig_MucklestonAsked:
Who is Participating?
 
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:

<script>

function getOverlays(theBox){
  if(theBox.checked==true){
    // do this
  } else {
   // do that
  }
}

</script>


0
 
mvan01Commented:
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
0
 
dbrittCommented:
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:

<script>

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

</script>

<input type="checkbox" id="chkA" onClick="getOverlays(this.checked)">
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.