0

I have checkbox on the page. I am wrapping that checkbox in a div and insert label after checkbox using javascript. Now I want to add all these elements on the body but it does not work. here is my code. I use

        document.body.innerHTML = wrapper;

in order to add those elements to body but it does not work. How can I make it work?

2
  • I don't understand, you're already using appendChild() on the wrapper. What makes you think appending to the <body> element should be done differently? Commented Feb 16, 2014 at 13:21
  • I want to show that checkbox. That wrapper is created using javascript which means it is not yet on the body. After insert two new elements inside wrapper now I want to show it to users to see. How can I show it? Commented Feb 16, 2014 at 13:23

2 Answers 2

2

You only have to apply appendChild() to the <body> element, just like you're already doing with the wrapper element:

document.body.appendChild(wrapper);

innerHTML is a string property that is supposed to be assigned raw HTML markup, not DOM elements.

You will find an updated fiddle here.

Sign up to request clarification or add additional context in comments.

Comments

1

Instead use document.body.innerHTML = wrapper; please use appendChild:

wrapper.appendChild(c[i]);
wrapper.appendChild(label);
document.body.appendChild(wrapper);

See this.

2 Comments

Now when I check the checkbox it doesnt get checked. Why?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.