In JavaScript, the Selection and Range objects allow you to interact with the selected text or elements on a web page, enabling features like text highlighting, editing, or manipulating selected content. These two concepts are closely related and often used together when building rich text editors, managing selections, or working with content in a browser.
Let's break down Selection and Range in detail:
1. The Selection
Object
The Selection
object represents the currently selected text or content in the document. This object is often used to get or manipulate the user's selection, such as the text highlighted by the user in a text field or the selection inside a content-editable element.
How It Works:
- The
Selection
object is part of the browser’swindow.getSelection()
method. - It holds information about the selected text and the range (or position) of that selection.
- The selection can span across multiple elements or be contained in a single element.
- It can be used for a variety of purposes, such as retrieving the selected text, changing the selection, or executing actions on the selected content.
Key Properties of Selection
Object:
anchorNode
: The starting node of the selection (where the selection began).focusNode
: The ending node of the selection (where the selection ends).anchorOffset
: The character offset within theanchorNode
where the selection begins.focusOffset
: The character offset within thefocusNode
where the selection ends.rangeCount
: The number of ranges currently in the selection (usually 1, but can be more in complex cases).toString()
: Returns the selected text as a string.
Basic Syntax to Access Selection:
Manipulating the Selection:
You can also use the Selection
object to modify the selection programmatically.
-
Remove the selection:
-
Add a Range to the selection:
2. The Range
Object
The Range
object represents a specific portion of a document. It can be used to define a selection’s start and end points, or to modify parts of a document by manipulating the DOM.
How It Works:
- A
Range
represents a contiguous portion of a document, and it can be used to describe the start and end of a selection. - It can be used independently or in conjunction with the
Selection
object. - A range is often used to highlight text, create bookmarks, or perform other DOM manipulations like applying styles or inserting content.
Key Methods of the Range
Object:
setStart(node, offset)
: Sets the start point of the range to a specific node and offset within that node.setEnd(node, offset)
: Sets the end point of the range to a specific node and offset.collapse(toStart)
: Collapses the range to a single point (either the start or the end).cloneRange()
: Creates a copy of the range.extractContents()
: Removes the content in the range from the document and returns it as a document fragment.insertNode(node)
: Inserts a node at the end of the range.
Creating and Using a Range:
Practical Use Cases for Range
and Selection
:
-
Highlighting Text:
- You can use a
Range
to programmatically highlight selected text by changing its background color.
- You can use a
-
Inserting HTML at the Cursor:
- You can insert HTML content at the cursor position by creating a
Range
object and usinginsertNode()
.
- You can insert HTML content at the cursor position by creating a
-
Cloning a Range:
- You can clone a range to work with its content without modifying the original selection.
-
Extracting Contents from a Range:
- Extracting the contents of a range allows you to remove a portion of the document and work with it.
Example: Using Selection
and Range
Together
Here’s a simple example that combines both Selection
and Range
objects to highlight selected text in a document:
Explanation:
- The user selects some text within the paragraph.
- The button (
highlightBtn
) is clicked to trigger the selection and range handling. - The
window.getSelection()
method retrieves the current selection. - The
getRangeAt(0)
method gets the first range (in case of multiple ranges). - The selected text is wrapped in a
<span>
element with a yellow background to highlight it.
Summary:
- Selection Object: Represents the currently selected content on the page (text or elements).
- Methods:
getSelection()
,removeAllRanges()
,addRange()
- Properties:
anchorNode
,focusNode
,toString()
- Methods:
- Range Object: Defines a contiguous portion of the document, used for working with selections and modifying content.
- Methods:
setStart()
,setEnd()
,insertNode()
,extractContents()
- Usage: Highlighting text, inserting content, extracting text, manipulating selections.
- Methods:
These objects are fundamental when building interactive applications that need to manipulate text or handle complex selections in documents.