0

I am trying to measure the length of text (based on this: Calculating text width) and then slice it at a certain point.

Here's my code, what am I missing?

$(document).ready(function() {
    $.fn.textWidth = function() {
        var html_org = $(this).html();
        var html_calc = "<span>" + html_org + "</span>"
        $(this).html(html_calc);
        var width = $(this).find("span:first").width();
        $(this).html(html_org);
        return width;
    };

    function sliceTxt() {
        var w = $(".limitBox").textWidth() * 2,
            textArray = $(".limitBox").text().split(""),
            ellipsedText = textArray.slice(0, w - 3).join("") + "...";

    }
    sliceTxt();
});

DEMO

Community
  • 1
  • 1
santa
  • 11,716
  • 43
  • 149
  • 239
  • It looks like you haven't sent it a text string. Maybe you can post more code? Looks OK, though. What is it doing wrong? – orolo Nov 08 '11 at 22:35
  • 1
    Please see DEMO link below my code here. It is a working demo on jsFiddle. Thx. – santa Nov 08 '11 at 22:37
  • Can't you use substring instead of converting the string to an array? http://www.w3schools.com/jsref/jsref_substring.asp – Steve Wellens Nov 08 '11 at 23:44
  • Not quite... My string needs to be about twice the length of the allocated area. If I use substring it assumes that each char = 1px. Here's an example: http://jsfiddle.net/RGybB/28/ – santa Nov 09 '11 at 17:15

0 Answers0