0

I have a script:

  _content_insert: function () {
        var e = this,
            t = this.$tooltip.find(".tooltipster-content");        
        if (typeof e.Content === "string" && !e.options.contentAsHTML) {
            t.text(e.Content)
        } else {
            t.empty().append(e.Content)
        }
    },

and I'd like to truncate the Content to 140 characters.

I found a truncating script here - Truncate a string straight javascript

var length = 3;
var myString = "ABCDEFG";
var myTruncatedString = myString.substring(0,length);

How do I implement this into the code so that what comes out in (e.Content) is the truncated 140-character string?

Community
  • 1
  • 1
Jony
  • 69
  • 1
  • 4

2 Answers2

0
t.text(e.Content.substring(0,140));

Doesn't it work?

_content_insert: function () {
    var e = this,
        t = this.$tooltip.find(".tooltipster-content");        
    if (typeof e.Content === "string" && !e.options.contentAsHTML) {
        t.text(e.Content.substring(0,140))
    } else {
        t.empty().append(e.Content.substring(0,140))
    }
},

You can optimize the code. You can truncate after the string check and use that var.

Gibbs
  • 21,904
  • 13
  • 74
  • 138
0

Try this...

_content_insert: function () {
    var e = this,
        t = this.$tooltip.find(".tooltipster-content");        
    if (typeof e.Content === "string" && !e.options.contentAsHTML) {
        if(e.Content.length>140){
           t.text(e.Content.substring(0,140))
        }
        else{
           t.text(e.Content);
        }
    } else {
        t.empty().append(e.Content)
    }
},
LoopCoder
  • 172
  • 6