Skip to main content
small changes in response to comments
Source Link
chmurson
  • 619
  • 6
  • 11

Neither Rune's nor Brain's was working for me in case when the element that was holding the text had fixed width. I did something similar to Okamera. It uses less selectors.

EDIT: It won't probably work for elements that uses relative font-size, as following code inserts htmlCalc element into body thus looses the information about parents relation.

$.fn.textWidth = function() {   var html_calchtmlCalc = $('<span>' + $(this).html() + '</span>');   html_calchtmlCalc.css('font-size',$( this).css('font-size')) .hide(); html_calc .prependTo('body');   var width = html_calchtmlCalc.width();   html_calchtmlCalc.remove();   return width;  }; 

Neither Rune's nor Brain's was working for me in case when the element that was holding the text had fixed width. I did something similar to Okamera. It uses less selectors.

$.fn.textWidth = function(){   var html_calc = $('<span>' + $(this).html() + '</span>');   html_calc.css('font-size',$(this).css('font-size')).hide(); html_calc.prependTo('body');   var width = html_calc.width();   html_calc.remove();   return width;  } 

Neither Rune's nor Brain's was working for me in case when the element that was holding the text had fixed width. I did something similar to Okamera. It uses less selectors.

EDIT: It won't probably work for elements that uses relative font-size, as following code inserts htmlCalc element into body thus looses the information about parents relation.

$.fn.textWidth = function() { var htmlCalc = $('<span>' + this.html() + '</span>'); htmlCalc.css('font-size', this.css('font-size')) .hide()  .prependTo('body'); var width = htmlCalc.width(); htmlCalc.remove(); return width; }; 
I added font-size css setter. The width of text is not calculated properly when font's css settings have changed after prepending to <body>.
Source Link
chmurson
  • 619
  • 6
  • 11

Neither Rune's nor Brain's was working for me in case when the element that was holding the text had fixed width. I did something similar to Okamera. It uses less selectors.

$.fn.textWidth = function(){ var html_calc = $('<span style="display:none">''<span>' + $(this).html() + '</span>'); html_calc.css('font-size',$(this).css('font-size')).hide(); html_calc.prependTo('body'); var width = html_calc.width(); html_calc.remove(); return width; } 

Neither Rune's nor Brain's was working for me in case when the element that was holding the text had fixed width. I did something similar to Okamera. It uses less selectors.

$.fn.textWidth = function(){ var html_calc = $('<span style="display:none">' + $(this).html() + '</span>'); html_calc.prependTo('body'); var width = html_calc.width(); html_calc.remove(); return width; } 

Neither Rune's nor Brain's was working for me in case when the element that was holding the text had fixed width. I did something similar to Okamera. It uses less selectors.

$.fn.textWidth = function(){ var html_calc = $('<span>' + $(this).html() + '</span>'); html_calc.css('font-size',$(this).css('font-size')).hide(); html_calc.prependTo('body'); var width = html_calc.width(); html_calc.remove(); return width; } 
deleted 3 characters in body
Source Link
chmurson
  • 619
  • 6
  • 11

Neither Rune's nor Brain's was working for me in case when the element that was holding the text had fixed width. I did something similar to Okamera. It uses less selectors.

$.fn.textWidth = function(){ var html_calc = $('<span style="display:none">' + $(this).html() + '</span>'); html_calc.prependTo('body'); var width = $(html_calc).width(); html_calc.remove(); return width; } 

Neither Rune's nor Brain's was working for me in case when the element that was holding the text had fixed width. I did something similar to Okamera. It uses less selectors.

$.fn.textWidth = function(){ var html_calc = $('<span style="display:none">' + $(this).html() + '</span>'); html_calc.prependTo('body'); var width = $(html_calc).width(); html_calc.remove(); return width; } 

Neither Rune's nor Brain's was working for me in case when the element that was holding the text had fixed width. I did something similar to Okamera. It uses less selectors.

$.fn.textWidth = function(){ var html_calc = $('<span style="display:none">' + $(this).html() + '</span>'); html_calc.prependTo('body'); var width = html_calc.width(); html_calc.remove(); return width; } 
Source Link
chmurson
  • 619
  • 6
  • 11
Loading