0

How can one express a condition within the parentheses if a checkbox is checked in JavaScript. Ex:

if(checkbox is checked) {
 execute function...
 }
 else {
      if(checkbox is not checked) {         /*otherwise*/
           execute alternate function...
      }
 }

I'm a total beginner with JavaScript. Of all the searching I've done, I've found more simpler examples with numbers and operators and such, but nothing with checkboxes.

Basically, what I'm trying to accomplish is to toggle a an element by checking and unchecking a checkbox.

I'd really appreciate any help anyone can lend.

alessandrio
  • 4,282
  • 2
  • 29
  • 40
Jay
  • 3
  • 3
  • 1
    Possible duplicate of [How to check whether a checkbox is checked in jQuery?](https://stackoverflow.com/questions/901712/how-to-check-whether-a-checkbox-is-checked-in-jquery) – Toan Tran May 28 '17 at 22:55
  • a checkbox element ( radio also) has a `checked` property that is true/false – charlietfl May 28 '17 at 23:08

1 Answers1

0

HTML

<input type="checkbox" id="goodId" name="whateverName" />

JAVASCRIPT

if (document.getElementById("goodId").checked = true) {

     // your function
}
Oliver
  • 11,857
  • 2
  • 36
  • 42
  • Thanks a lot Joseph. It works. Another thing I'm trying to figure out, how would you express two or more conditions. Would it be any thing like this: var a = 10; var b = 5; function c() { if (a + b == 15 && a - b == 5) { //function c() } } – Jay May 30 '17 at 16:40