JavaScript Code

How to remove paragraph using JavaScript?


Tutorials using JS

Problem Statement

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>

Copyright @2022