Catalog / DOM Selection API Cheatsheet
DOM Selection API Cheatsheet
A quick reference guide to the HTML DOM Selection API, covering essential methods, properties, and event handling for managing text selections in web applications.
Selection Basics
Obtaining the Selection Object
Use
|
Selection Object Properties
|
The node in which the selection begins. |
|
The offset into the |
|
The node in which the selection ends. |
|
The offset into the |
|
A boolean indicating whether the selection’s start and end points are at the same position. |
|
The number of ranges in the selection. |
Example
|
Manipulating Selections
Adding and Removing Ranges
|
Adds a |
|
Removes a |
|
Removes all ranges from the selection, effectively deselecting everything. |
Creating and Using Ranges
|
Example: Selecting part of a text node.
|
Selection State Manipulation
Collapsing Selections
|
Collapses the selection to a single point at the specified |
|
Collapses the selection to its start point. |
|
Collapses the selection to its end point. |
Checking Node Containment
|
Advanced Selection Techniques
Deleting Content from the Document
Example: Delete selected text when a button is clicked.
|
Listening for Selection Changes
Use the
|