user2953119
user2953119

Reputation:

How to add element inside the div

Let I've created an element as the following:

var elem= document.createElement("p");

And I've a div#parent element. How can I put elem inside the div#parent? Is it possible to do without jQuery?

Upvotes: 0

Views: 291

Answers (6)

SeeMeCode
SeeMeCode

Reputation: 1435

Nope, have to use jQuery. That's your only option. :)

Just kidding.

Something like this should work:

var parent = document.getElementById('parent');
parent.appendChild(elem);

It's that simple. Even without using good 'ole jQuery!

Upvotes: 2

Ramesh
Ramesh

Reputation: 4293

var parent = document.getElementById("parent");
parent.appendChild(elem);

Upvotes: 0

Shaik Mahaboob Basha
Shaik Mahaboob Basha

Reputation: 1082

You have to use appendChild method to add / append a child.

document.getElementById('parent').appendChild( document.createElement("p") );

More Info:

appendChild

Upvotes: 2

user13500
user13500

Reputation: 3856

By appendChild()

var elem = document.createElement("p");
var div  = document.getElementById("parent");
div.appendChild(elem);

And to alaborate. jQuery is written in pure Javascript. Anything jQuery do, you can do. That said, there are various quirks etc. that it abstract away.

Upvotes: 0

Sterling Archer
Sterling Archer

Reputation: 22395

Of course you can!

var parent = document.querySelector("div#parent");
//or document.getElementById("parent");
//I doubt speed is an issue so both will work the same
parent.appendChild(elem);

Upvotes: 0

techfoobar
techfoobar

Reputation: 66663

Yes, you can use appendChild():

var parent = document.getElementById('parent');
var elem= document.createElement("p");
parent.appendChild(elem);

Upvotes: 0

Related Questions