Skip to content

Instantly share code, notes, and snippets.

@shaneparsons
Last active April 27, 2016 18:17
Show Gist options
  • Save shaneparsons/68b15108adbf3216e2f36030c1e7d804 to your computer and use it in GitHub Desktop.
Save shaneparsons/68b15108adbf3216e2f36030c1e7d804 to your computer and use it in GitHub Desktop.
jQuery - Calculate height of text from DOM element or string.
$.fn.textHeight = function (text, font, transform) {
if (!$.fn.textWidth.fakeEl) $.fn.textWidth.fakeEl = $('<span>').hide().appendTo(document.body);
$.fn.textWidth.fakeEl.text(text || this.val() || this.text())
.css('font', font || this.css('font'))
.css('text-transform', transform || this.css('text-transform'));
return $.fn.textWidth.fakeEl.height();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment