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

123
Vistas
Labels wrapping input checkbox

Generally, I like this style of label and input:

<label><input type="checkbox"> Click me!</label>

Then I can click both the box and the label. But I toyed with creating things this way:

let lab = document.createElement('label');
let cb = document.createElement('input');
cb.type = "checkbox";
lab.appendChild(cb);

There is where I got stuck. If I use lab.innerText="anything" the checkbox goes away. It's that or put the label before or after the checkbox and then you can't click on the label to change the checkbox. I may be screwing up the HTML. I'm not sure. In the end, I had to use lab.innerHTML to add the checkbox. That works, but I know it's not proper. Then I could use lab.querySelector to get the checkbox and attach an onclick event.

There's got to be a better way.

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

0

And the answer is YES to my comment!

We can insert a text node before the checkbox is inserted.

Thankfully there is a shortcut to inserting a text node, which is just inserting a string with append:

let lab = document.createElement('label');

let cb = document.createElement('input');

cb.type = "checkbox";

lab.append("label here");

lab.appendChild(cb);

document.body.appendChild(lab);

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