Dom Selection Cheat Sheet

From WikiOD

Here is the cheatsheat of Dom Selection and basic guide on how to use Dom Selection

Reference[edit | edit source]

{: .-three-column}

Selection[edit | edit source]

var sel = document.getSelection()

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

Methods[edit | edit source]

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[edit | edit source]

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

Deleting[edit | edit source]

sel.deleteFromDocument()

Events[edit | edit source]

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

Credit:rstacruz