Jared
Jared

Reputation: 71

How to create an element in JavaScript, under a specific HTML tag

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

Answers (2)

Ethaan
Ethaan

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

Gorige ashok kumar
Gorige ashok kumar

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

Related Questions