Reputation:
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
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
Reputation: 4293
var parent = document.getElementById("parent");
parent.appendChild(elem);
Upvotes: 0
Reputation: 1082
You have to use appendChild method to add / append a child.
document.getElementById('parent').appendChild( document.createElement("p") );
More Info:
Upvotes: 2
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
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
Reputation: 66663
Yes, you can use appendChild()
:
var parent = document.getElementById('parent');
var elem= document.createElement("p");
parent.appendChild(elem);
Upvotes: 0