Search code examples
javascriptjqueryclassqtip2

qTip2 - change which tooltip content is used on class change


I am using qTip2, and I have a list of links that have tooltips on them. I read tooltips content from an attribute in my link called "data-normal". There is a checkbox next to each link, and when it's clicked it removes the class for that link and adds some other class. When the link is changed like that, I would like the tooltip to stay but to be read from a different attribute ("data-changed").

Also, when the box is unchecked (and classes reverted) I would like it to function like before the change.

Is there a solution for this?

Here is the piece of code I use for this:

$('.normal').qtip({
    content: {
    text: function(api) {
        return $(this).attr('data-normal');
        }
    }
});

$('.changed').qtip({
    content: {
    text: function(api) {
        return $(this).attr('data-changed');
        }
    }
});

Here is a jsfiddle of my problem: http://jsfiddle.net/trunkadelic/Xkvnt/1/

This is of course a simplified example of what I'm trying to do on the website itself.


Solution

  • $("a").qtip({
        content: {
            text: function(api) {
                if ($(this).hasClass('changed')) {
                    return $(this).attr('data-changed');
                } else {
                    return $(this).attr('data-normal');
    
                }
            }
        }
    });
    

    this should work. Here is your updated example: http://jsfiddle.net/Xkvnt/5/