Tutorials using JS
Create a Paragraph element using JavaScript
In this tutorial, you shall learn how to create a paragraph element using JavaScript, and add it to the document.
Solution
To create a paragraph element using JavaScript, we can use document.createElement()
method. We have to pass "p"
string to the method to specify that we are creating a paragraph element.
document.createElement("p")
Once we create a paragraph element, we can add some text in it using innerText
property .
Program
When user clicks on the button, we create a paragraph element and append it as a child to the div#output
element.
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Tutorial</h1>
<div id="output"></div>
<script>
const myPara = document.createElement("p");
myPara.innerText = "This is a paragraph created using JavaScript.";
document.getElementById("output").appendChild(myPara);
</script>
</body>
</html>