title | category | intro |
---|---|---|
DOM Selection |
JavaScript |
Quick introduction to the HTML [DOM selection API](https://devdocs.io/dom/selection).
|
{: .-three-column}
var sel = document.getSelection()
See: https://devdocs.io/dom/selection
sel.removeAllRanges() // deselects
sel.addRange(range) // sets a selection
sel.removeRange(range) // remove a range
sel.rangeCount
sel.getRangeAt(0) // get the 0th range
sel.collapse(parent, offset)
sel.collapseToEnd()
sel.collapseToStart()
sel.isCollapsed
sel.containsNode(node)
sel.deleteFromDocument()
document.addEventListener('selectionchange', () => {})