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

x
  • 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?

0
Craig_Muckleston
Asked:
Craig_Muckleston
1 Solution
 
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
 
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
 
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

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