DEV Community

Lam
Lam

Posted on

1 1

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

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay