1
0
mirror of https://github.com/onkelbeh/cheatsheets.git synced 2025-10-15 15:08:27 +02:00
cheatsheets/dom-selection.md
Rico Sta. Cruz 511de900ba
Formatting updates (#2133)
- Update some sheets which have very long sections
- Remove `layout: 2017/sheet` (everything has the same layout now)
- Remove outdated sheets
2024-04-03 18:30:24 +11:00

766 B

title, category, intro
title category intro
DOM Selection JavaScript 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', () => {})