Skip to content

Instantly share code, notes, and snippets.

@AndreasMadsen
Created October 29, 2012 13:57
Show Gist options
  • Save AndreasMadsen/3973705 to your computer and use it in GitHub Desktop.
Save AndreasMadsen/3973705 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
@kesla
Copy link

kesla commented Oct 29, 2012

dude, that's awesome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment