DOM Range

Quick reference to the HTML DOM createRange API.

A cheatsheet by @rstacruz|Refreshed 7 months ago.Refresh|View source on Github
range
  .setStart(startNode, startOffset)
  .setEnd(endNode, endOffset)

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

  .selectNode(node)
  .selectNodeContents(node)

Collapsing

Collapsing

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

Operations

Operations

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

String

String

range.toString()

Read-only attributes

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