Open In App

HTML DOM deleteFromDocument() method

Last Updated : 21 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

Syntax:

selectedText.deleteFromDocument()

Parameters:

  • No parameters

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

HTML




<!DOCTYPE html>
<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>
 
    <script>
        let btn = document.querySelector('button');
        btn.addEventListener('click', del);
        function del() {
            let sel = window.getSelection();
            sel.deleteFromDocument();
        }
    </script>
</body>
 
</html>


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

Supported Browsers:

  • Google Chrome
  • Edge
  • Firefox
  • Opera
  • Safari

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads