Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

120
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!