0

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?

0

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

Comments

0
<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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.