problem with width() and space between words
in Using jQuery
•
9 years ago
Here is my code (copied from some kind soul somewhere on the internet -- this is part of a function to loop through child elements to get maximum width):
var html_org = $(this).html();
var html_calc = '<span>' + $(this).text() + '</span>';
$(this).html(html_calc);
var w = $(this).find('span:first').innerWidth();
$(this).html(html_org);
where $(this) is the label element for:
<input type="radio" id="radio5" name="radio" value="MembersPayments"/><label for="radio5">Members Payments</label>
The result of the above is w = 54 (textlength = 7) -- the second word is ignored.
If I replace the space between "Members" and "Payments" with "$nbsp;":
<input type="radio" id="radio5" name="radio" value="MembersPayments"/><labelfor="radio5">Members Payments</label>
the result is 104 (textlength = 16) -- the correct result.
I would like to be able to use regular spaces -- what am I doing wrong?
Thanks.
1