I\'m trying to calculate text width using jQuery. I\'m not sure what, but I am definitely doing something wrong.
So, here is the code:
var c = $(\'.c
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 = $('' + this.html() + '');
htmlCalc.css('font-size', this.css('font-size'))
.hide()
.prependTo('body');
var width = htmlCalc.width();
htmlCalc.remove();
return width;
};