JAVASCRIPT - HOW TO Delete/Remove Selected Text - Javascript Source Code
JAVASCRIPT - HOW TO Delete or Remove Selected Text when Text on a Web Page is Selected and the Delete Key is Pressed
Function to deleted selected text on a web page when the DELETE key is pressed. This allows you to systematically deleted selected text anywhere on the web page thus changing the appearance of the web page.
To delete selected text on a web page when the delete key is press involves ...
- Creating an event listener that listens for a
keydown
event.
- The event function checks to see if the key that was pressed is the DELETE key.
- If the DELETE key was pressed, the function then checks to see if any text on the web page is selected. If text on the web page is selected, the function ...
- Gets the parent element of the selected text.
- Gets the entire text content of the parent element.
- Replaces the select text with an empty string, in essence, subtracting the selected text from the whole text of the parent element.
- The text content of the parent element of the selected text is set to this new text, with the selected text removed.
Deleting selected text when the DELETE key is pressed sounds more complicated than it really is.
Function to Check for when Delete Key is Pressed
Function to Determine what the Selected Text is and Remove it
To properly and consistenty get the selected text we want to be sure we use Javascript methods that are supported across all devices.
Code to Remove or Delete the Selected Text from the Entire Text of the Parent Object of the Selected Text
Complete Code to Remove Selected Text when Delete Key Pressed
Try Deleting Selected Text on this Page
Select any text on this page and press the DELETE key to see this delete selected text code for yourself. Feel free to right click the web page and view the Javascript source code.