Skip to content

Latest commit

 

History

History
72 lines (54 loc) · 1.17 KB

dom-range.md

File metadata and controls

72 lines (54 loc) · 1.17 KB
title category intro
DOM Range
JavaScript
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