Skip to content
Related Articles

Related Articles

Improve Article
HTML DOM deleteFromDocument() method
  • Last Updated : 03 Jul, 2020

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:




<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>

Output:

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

Supported Browsers:

  • Google Chrome
  • Edge
  • Firefox
  • Opera
  • Safari
My Personal Notes arrow_drop_up
Recommended Articles
Page :