My html
and Javascript is as below:
const container = document.getElementById('container')
const label = document.createElement('label')
const input = document.createElement('input')
input.type = 'checkbox'
input.checked = true
label.appendChild(input)
label.innerHTML += 'This is a label'
container.appendChild(label)
<html>
<body>
<div id = 'container'></div>
</body>
</html>
The result is that I just get an unchecked checkbox in the label. However, if I remove the label from the equation and just append the input to the container, then it is pre-checked as expected. How come setting input.checked = true
has no effect when the input is in a label?
const container = document.getElementById('container')
const input = document.createElement('input')
input.type = 'checkbox'
input.checked = true
container.appendChild(input)
<html>
<body>
<div id='container'></div>
</body>
</html>