mardi 11 février 2020

(Vanilla) JS: Checkbox "checked" returning unexpected results

Using the pen below you can see there are two elements, a button and a checkbox input.

When the button is clicked I would like to determine the status of checkbox (clicked/not-clicked).

Regardless of the visual check mark presence or not, the console always logs false. Why is this?

https://codepen.io/sterlingbutters/pen/ZEGGgvm

EDIT (code):

HTML:

<input id='stall' type="checkbox">

<button id='button'>Test

JS:

var stall = document.getElementById('stall').checked;
var button = document.getElementById('button');
button.onclick = function () {
  console.log(stall);
}



Aucun commentaire:

Enregistrer un commentaire