I had trouble with solutions like @rune-kaagaard's for large amounts of text. I discovered this:
$.fn.textWidth = function() { var width = 0; var calc = '<div'<span style="widthstyle="display: block; width: 100%; overflow-y: scroll; white-space: nowrap;">'nowrap;" class="textwidth"><span>' + $(this).texthtml() + '</div>';span></span>'; $('body').append(calc); var last = $('body').find('div'span.textwidth:last'); var width = last.propif ('scrollWidth'last) ?{ var lastcontent = last.propfind('scrollWidth''span'); : last width = lastcontent.width(); last.remove(); } return width; };