- Source:
Methods
createSelection(node, startOffsetopt, nullable, endOffsetopt, nullable) → {String}
Programmatically create a text selection inside a node, possibly reaching across several child nodes, but virtually only working with the raw text content. Can also be used to create a selection in text inputs for example.
Be aware that the endOffset is neither the length to select nor the last index, but the offset starting from the last character in the node counting backwards (like a reverse startOffset). The reason for this wonkyness is the fact that we have to implement three different ways of creating selections in this function, this notation being the most compatible one. We assume the default use case for this method is to select all content of a node with the possibility to skip one or two unwanted characters on each side of the content.
Hint: You cannot create a selection spanning normal inline text into an input, ending there. To create a selection in a text input, please target that element specifically or make sure the selection spans the whole input. Furthermore, on mobile/iOS devices creation of selection ranges might only be possible in text inputs.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
node |
HTMLElement | the element to create a selection inside |
||
startOffset |
Number |
<optional> <nullable> |
0 | characters to leave out at the beginning of the text content |
endOffset |
Number |
<optional> <nullable> |
0 | characters to leave out at the end of the text content |
- Source:
- See:
Returns:
- Type
- String
Example
const selectedText = createSelection(copytextElement, 12, 6);