Skip to main content
added 143 characters in body
Source Link

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; };

JSFiddleJSFiddle GitHub

I had trouble with solutions like @rune-kaagaard's for large amounts of text. I discovered this:

$.fn.textWidth = function() { var calc = '<div style="width: 100%; white-space: nowrap;">' + $(this).text() + '</div>'; $('body').append(calc); var last = $('body').find('div:last'); var width = last.prop('scrollWidth') ? last.prop('scrollWidth') : last.width(); last.remove(); return width; };

JSFiddle

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 = '<span style="display: block; width: 100%; overflow-y: scroll; white-space: nowrap;" class="textwidth"><span>' + $(this).html() + '</span></span>';   $('body').append(calc);   var last = $('body').find('span.textwidth:last'); if (last) {  var lastcontent = last.find('span');   width = lastcontent.width();   last.remove();   } return width; };

JSFiddle GitHub

Source Link

I had trouble with solutions like @rune-kaagaard's for large amounts of text. I discovered this:

$.fn.textWidth = function() { var calc = '<div style="width: 100%; white-space: nowrap;">' + $(this).text() + '</div>'; $('body').append(calc); var last = $('body').find('div:last'); var width = last.prop('scrollWidth') ? last.prop('scrollWidth') : last.width(); last.remove(); return width; };

JSFiddle