Tutorials using JS
Remove paragraph using JavaScript
In this tutorial, you shall learn how to remove paragraph element from the document using JavaScript.
Solution
To remove a paragraph element from document using JavaScript, we can use remove()
method on the element. Get the paragraph element, and call remove()
method.
paragraphElement.remove()
remove()
method removes the calling element from the document.
Program
In the HTML, we have a Click me
button and paragraph. When user clicks on the Click me
button, we get the paragraph element by id myPara
and remove it from the document using remove()
.
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Tutorial</h1>
<button id="myBtn">Click me</button>
<p id="myPara">This is a paragraph. Welcome to JavaScript tutorials.</p>
<script>
document.getElementById("myBtn").addEventListener("click", function() {
//get the paragraph element
const myPara = document.getElementById("myPara");
//remove the paragraph
myPara.remove();
});
</script>
</body>
</html>