Tutorials using JS
Problem Statement
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
<!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>