qTip show/hide tooltip on click

Bes-m M-bes picture Bes-m M-bes · Apr 5, 2012 · Viewed 9.2k times · Source

I have a page with many tooltips. I want to show/hide every one seperatly on clicking a button. It does not work for me, can anyone help?

<span><input type="button" value="b1" onClick="showMyTT('div1');"></span><span><div id="div1"></div></span>
<span><input type="button" value="b2" onClick="showMyTT('div2');"></span><span><div id="div2"></div></span>
<span><input type="button" value="b3" onClick="showMyTT('div3');"></span><span><div id="div3"></div>​</span>

function showMyTT(id) { 
    $('#'+id).qtip({
        content: {
            text: 'Show the content', 
            title: false
        },
        position: {
            my: 'left center', 
            at: 'left center' 
        },
        show: {
            event: false, 
            ready: true 
        },
        hide: false, 
        style: {
            classes: 'ui-tooltip-shadow ui-tooltip-jtools'
        }
    }); 
}

Thank you

Answer

freakish picture freakish · Apr 5, 2012

This showMyTT function initializes your tooltips. After running this function for all ids (initialization) you should do something like this:

HTML

<input type="button" value="b1" onClick="toggleQtip('div1');" />

JavaScript

function toggleQtip(id) {
    var div = $('#'+id);
    if (div.data('visible')) {
        div.qtip('hide');
        div.data('visible', false);
    } else {       
        div.qtip('show');
        div.data('visible', true);
    }
}

EDIT

Here's how you can initialize qtip (at least one possibility). Add custom class to all divs you'd like to have a qtip, for example qtiped:

HTML

<div id="div1" class="qtiped">TEST</div>
<input type="button" value="b1" onClick="toggleQtip('div1');" />

and then run the following code:

JavaScript

$(document).ready(function() {
    $('.qtiped').each(function() {
        showMyTT( $(this).attr('id') );
    });
});

Also, inside showMyTT do the following change: show: false in order to avoid autoloading (we want to initialize tooltips, not show them).