Skip to content

Instantly share code, notes, and snippets.

@dylansm
Forked from AndreasMadsen/hackTheDom.js
Created March 10, 2019 06:44
Show Gist options
  • Save dylansm/857add123c57e3911b748fa3ee0f23d8 to your computer and use it in GitHub Desktop.
Save dylansm/857add123c57e3911b748fa3ee0f23d8 to your computer and use it in GitHub Desktop.
size of TextNode - DOM Hack
// This is the coolest code snippet I (Mr. Mad) have every created
// It will magicly get the size of a textnode, by createing a context
// range and get the client rects of that range.
// It will then combine the rects to one rect, and thereby get the size
// of the text node.
// Note that by any standarts a TextNode has no size.
var range = document.createRange();
range.selectNode(node);
var rect = range.getBoundingClientRect();
// The magic ends here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment