jonnywalker
jonnywalker

Reputation: 21

Plugin tooltipster - Trunkate Title

I am using the plugin "tooltipster" but I would like to truncate the title to 30 characters and add hellips.

I have a list of 3 links. Below is the code and added a link to example

$('.tooltip').tooltipster({
    animation: 'fade',
    delay: 200,
    touchDevices: false,
    trigger: 'hover',
    position: 'bottom',
    theme: 'tooltipster-shadow'
});


$('.box a').each(function(){
    if ($(this).attr('title').text().length > 20) {
        $(this).attr('title').text($(this).text().substr(0, 17)); 
        $(this).attr('title').append(' ...');
    }

});

http://jsfiddle.net/rttUG/

thank you very much!

Upvotes: 0

Views: 272

Answers (1)

hutchbat
hutchbat

Reputation: 796

  1. You should execute your scripts after the dom is ready, use $(document).ready(function(){}) or $(function(){})
  2. To get attribute value use $.attr('attribute') instead of $.attr('attribute').text()
  3. To update the attribute value use $.attr('attribute', 'new value') instead of $.attr('attribute').text('new value')

Your new code will be like this:

$(function(){

    $('.box a').each(function(){
        var title = $(this).attr('title');
        if (title.length > 20) {
            $(this).attr('title', title.substr(0, 17) + '...'); 
        }
    })

    $('.tooltip').tooltipster({
        animation: 'fade',
        delay: 200,
        touchDevices: false,
        trigger: 'hover',
        position: 'bottom',
        theme: 'tooltipster-shadow'
    });

})

http://jsfiddle.net/8vpUk/

Upvotes: 1

Related Questions