Skip to content

Instantly share code, notes, and snippets.

@sinslav
Created November 17, 2015 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sinslav/835fe4b20354da4be2b3 to your computer and use it in GitHub Desktop.
Save sinslav/835fe4b20354da4be2b3 to your computer and use it in GitHub Desktop.
$.fn.textWidth = (text, font) ->
if (!$.fn.textWidth.fakeEl)
$.fn.textWidth.fakeEl = $('<span>').css("white-space", "pre").hide().appendTo(document.body)
$.fn.textWidth.fakeEl.text(text or this.val() or this.text()).css('font', font or this.css('font'))
return $.fn.textWidth.fakeEl.width()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment