HTML DOM deleteFromDocument() method

The deleteFromDocument() method deletes the selected text from the document’s DOM tree.

Syntax:

selectedText.deleteFromDocument()

Parameters:

  • No parameters

Example:

The example delete the selected text by clicking a button. Upon clicking the button, the Window.getSelection() method gets the selected text, and with the help of deleteFromDocument() method removes it.



Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
   <head>
      <title>GeeksforGeeks</title>
      </head>
      <body>
          <h1>GeeksforGeeks</h1>
        <p>select some text and click on
 button to delete it from document</p>
        <button>Click</button>
      </body>
      <script>
          let btn = document.querySelector('button');
          btn.addEventListener('click', del);
  
          function del() {
            let sel = window.getSelection();
            sel.deleteFromDocument();
          }
      </script>   
</html>

chevron_right


Output:

Select some text and then click on button to delete it from document.

Supported Browsers:

  • Google Chrome
  • Edge
  • Firefox
  • Opera
  • Safari

full-stack-img

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.