All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

jQuery Remove Element

Last Updated : Mar 11, 2024

jQuery Remove Element

In this article we will show you the solution of jQuery remove element, here we are going to show you example for remove class from element with the help of remove() method.

The remove() method removes the selected elements, including all text and child nodes. So we can remove any selected elements in html document using this concept.

Step By Step Guide On jQuery Remove Element :-

Here we defined heading tag, two paragraph tags with some contents and at last button ‘Click Me’.

In script block we defined ready() method which is automatically starts loading code of within this function when this program opened on browser and within that defined click() method with element name ‘button’ so which is refers when clicks on button click() event will load particular process.

There we defined remove() method with no parameter with ‘.mainele’ of div tag it will removes div tag with child nodes.

<!DOCTYPE html>
<html>
    <head>
        <title>REMOVE ELEMENT USING JQUERY</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
        <script>
            $(document).ready(function(){
                $("button").click(function(){
                    $(".mainele").remove();
                });
            });
        </script>
    </head>
    <body>
        <style>
            .mainele{
                width: 50%;
                background-color: aquamarine;
                padding: 10px;
            }
        </style>
        <div class="mainele">
            <h1>Welcome All</h1>
            <p>This is first paragraph tag</p>
            <p>This is second paragraph tag</p>
            </div>
        <button>Click Me</button>
    </body>
</html>
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Here we imported open source jquery library support file which is needed for coding using jquery in program.
  5. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  6. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  7. In script we defined ready() and click() method within that we are goings to removing parent element of class ‘mainele’ div element in html code with child nodes of ‘h1, p’ by ‘remove()’ method without parameter.
  8. So which process will begin when user clicks on button ‘Click Me’ after loading this program on browser and in style block we defined three properties ‘background color as aquamarine, width to 50% of page, padding i.e inner space as 10px’ for class ‘mainele’.
  9. In html block we defined h1 tag with text ‘Welcome All’ and two para tags with respective contents ‘This is first paragraph tag, This is second paragraph tag’ and lastly button ‘Click Me’.
  10. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how to remove element from html document using jquery.

Before execution of program we needs to confirm internet connection because then only that external cdn link of jquery file will supports defined jquery codes without error.

When we executes program on browser we can see heading, two lines of text each line texts and a button ‘Click Me’, user needs to clicks on button then all elements will gone because we used remove() method its removed child nodes and text, styles from document.

I hope this article on jQuery remove element helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪