I try to do as describe in - check if checkbox is checked javascript
And this is my code -
var input = document.createElement("input");
input.name = "test";
input.type = "checkbox";
input.checked = true ;
var currentInput = document.getElementsByName("test");
if (currentInput.checked) {
console.log("true");
}
but after run it nothing logs to the console although the input.checked = true
assignment.
Why doesn't it logs true
?
Edit:
According to what @adeneo and @mori57 said , now the follow -
var input = document.createElement("input");
input.name = "test";
input.type = "checkbox";
input.checked = true;
var body = document.getElementsByTagName('body')[0];
body.appendChild(input);
var currentInput = document.getElementsByName("test")[0];
if (currentInput.checked) {
console.log("true");
}
logs true