Reputation: 71
I want to create a paragraph element from JavaScript, but I want it to be placed under a certain tag in HTML. How would I do this?
Upvotes: 0
Views: 289
Reputation: 11376
Try with this.
var element = document.createElement("p"); //div,span,h1
element.appendChild(document.createTextNode('the text you want if you want'));
document.getElementById('theElementToAppend').appendChild(element);
Upvotes: 1
Reputation: 176
<html>
<head>
<title>appendChild() Example</title>
<script type="text/javascript">
function appendMessage() {
var oNewP = document.createElement("p");
var oText = document.createTextNode("www.java2s.com");
oNewP.appendChild(oText);
document.body.appendChild(oNewP);
}
</script>
</head>
<body onload="appendMessage()">
</body>
</html>
Upvotes: 0