Skip to content

Latest commit

 

History

History
73 lines (55 loc) · 1.19 KB

dom-range.md

File metadata and controls

73 lines (55 loc) · 1.19 KB
title category layout intro
DOM Range
JavaScript
2017/sheet
Quick reference to the HTML [DOM createRange API](https://devdocs.io/dom/range).

Reference

{:.-three-column}

Creating ranges

var range = document.createRange()

See: https://devdocs.io/dom/document/createrange

Methods

range
  .setStart(startNode, startOffset)
  .setEnd(endNode, endOffset)

  .setStartBefore(node)
  .setStartAfter(node)
  .setEndBefore(node)
  .setEndAfter(node)

  .selectNode(node)
  .selectNodeContents(node)

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

Collapsing

range.collapse() // to end (a single point)
range.collapse(true) // to start (a single point)
range.collapsed // true | false

Operations

range.cloneContents() // copy => DocumentFragment
range.extractContents() // cut  => DocumentFragment
range.deleteContents() // delete
range.insertNode(node)

String

range.toString()

Read-only attributes

range.collapsed //       => true/false
range.startContainer //  => Node
range.startOffset
range.endContainer //    => Node
range.endOffset
range.commonAncestorContainer // closest of start and end containers