Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

143
Vistas
Input in a label created via javascript is not checked

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>

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

The line:

label.innerHTML += 'This is a label'

takes your existing innerHTML and re-writes it, destroying any (non-delegated) event-handlers or existing properties.

If, instead of label.appendChild(), you use:

label.append(input, 'This is a label');

Then it works to add the text as well as the <input> to the <label>:

const container = document.getElementById('container')

const label = document.createElement('label')
const input = document.createElement('input')

input.type = 'checkbox'
input.checked = true

label.append(input, 'This is a label');

container.appendChild(label)
<div id='container'></div>

References:

  • Element.append().
about 4 years ago · Juan Pablo Isaza Denunciar

0

In place of using innerHTML, which doesn't work well when adding siblings since it destroys and recreates the parent element, it's better using appendChild and add the sibling as a TextNode:

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.appendChild(document.createTextNode("This is a label")); 

container.appendChild(label)
<html>
<body>
<div id = 'container'></div>
</body>
</html>

This will also prevent script injection whether you'll decide, in the future, to allow users decide the label.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You're missing a ";" after each line, so that is a problem.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda