DEV Community

Lâm
Lâm

Posted on

Dom Selection Cheat Sheet

[Reference] Events

document.addEventListener('selectionchange', () => {})
Enter fullscreen mode Exit fullscreen mode

[Reference] Deleting

sel.deleteFromDocument()
Enter fullscreen mode Exit fullscreen mode

[Reference] Collapsing

sel.collapse(parent, offset)
sel.collapseToEnd()
sel.collapseToStart()
sel.isCollapsed
Enter fullscreen mode Exit fullscreen mode
sel.containsNode(node)
Enter fullscreen mode Exit fullscreen mode

[Reference] Methods

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

[Reference] Selection

var sel = document.getSelection()
Enter fullscreen mode Exit fullscreen mode

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

Reference

{: .-three-column}

Reference

Discussion (0)