Skip to content

Latest commit

 

History

History
56 lines (42 loc) · 785 Bytes

dom-selection.md

File metadata and controls

56 lines (42 loc) · 785 Bytes
title category layout intro
DOM Selection
JavaScript
2017/sheet
Quick introduction to the HTML [DOM selection API](https://devdocs.io/dom/selection).

Reference

{: .-three-column}

Selection

var sel = document.getSelection()

See: https://devdocs.io/dom/selection

Methods

sel.removeAllRanges() //  deselects
sel.addRange(range) //    sets a selection
sel.removeRange(range) // remove a range
sel.rangeCount
sel.getRangeAt(0) // get the 0th range

Collapsing

sel.collapse(parent, offset)
sel.collapseToEnd()
sel.collapseToStart()
sel.isCollapsed
sel.containsNode(node)

Deleting

sel.deleteFromDocument()

Events

document.addEventListener('selectionchange', () => {})