After reading http://stackoverflow.com/a/15302051/137067 I didn't see a function that was as good as it could be, so I made an improved one:
// Calculate width of text from DOM element or string. By Phil Freo <http://philfreo.com>
$.fn.textWidth = function(text, font) {
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'));
return $.fn.textWidth.fakeEl.width();
};
Used in Close.io to measure the width of the actual text typed inside our search box (which is actually a <textarea>) to determine when to expand the box wider.
It could probably be more accurate if you cloned the current element and appended it right next to it, hidden, so that it gets targeted by the exact same CSS rules as the target element. Then you wouldn't need to manually copy the css.