Last active
April 27, 2016 18:17
-
-
Save shaneparsons/ed7a5042758fd7c042359a58f998723e to your computer and use it in GitHub Desktop.
jQuery - Calculate width of text from DOM element or string.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.fn.textWidth = 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.width(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment