Aiden
Aiden

Reputation: 460

Creating HTML element inside a DIV

I have to create multiple checkboxes and display them in html at a specific place in the page.

I have created a div

<div id="htmlDiv">
</div>

and then created checkboxes in javascript (Ihave to create them dynamically),

function createNewCheckboxt(name, id){
var checkbox = document.createElement('input'); 
checkbox.type= 'checkbox';
checkbox.name = name;
checkbox.id = id;
return checkbox;
}

but how can I place these checkboxes in DIV i.e. at specific place in html page ?

Thanks

Aiden

Upvotes: 0

Views: 4933

Answers (1)

Gil Epshtain
Gil Epshtain

Reputation: 9801

You need to find the parent element, to which you wont to attach your element, then use the appendChild() method, for example:

var checkbox = createNewCheckboxt('name', 'id'); // method defined in the question     
var parent  = document.getElementById('htmlDiv');
parent.appendChild(checkbox);

reference: https://developer.mozilla.org/en-US/docs/Web/API/Node/appendChild

Upvotes: 1

Related Questions