0

Im having trouble getting the console to log when the checkbox has been selected or not.

HTML:

<label class="container">
   <input type="checkbox" class="checkbox1">
      <span class="checkmark"></span>
</label>

JS:

function carDealer () {

        if (document.getElementsByClassName('checkbox1').checked == true) {
            console.log('true');

            } else {
                console.log('false');
            }
    }
    carDealer();
Cam
  • 15
  • 1
  • 2
  • 6
  • 1
    `document.getElementsByClassName('checkbox1')[0]` `getElementsByClassName` gives out an array of DOM nodes. – Nandu Kalidindi Dec 09 '17 at 04:19
  • Possible duplicate of [Check if checkbox is checked JavaScript](https://stackoverflow.com/questions/9887360/check-if-checkbox-is-checked-javascript) – Junius L Dec 09 '17 at 04:23

1 Answers1

1

getElementsByClassName returns array like object, so please replace

document.getElementsByClassName('checkbox1').checked == true

to

document.getElementsByClassName('checkbox1')[0].checked == true
sapics
  • 1,104
  • 8
  • 22
  • 2
    or, simply to: `document.getElementsByClassName('checkbox1')[0].checked` (without the `== true`) – Nir Alfasi Dec 09 '17 at 04:30
  • thanks, problem solved! I also was expecting the console to automatically update upon checked/not checked but then I realized I needed to hit the submit button for it to update woops. – Cam Dec 09 '17 at 04:36