Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery UI Tooltip with data attributes

I'm attempting to use HTML5 data attributes to store and display content for a tooltip.

I'm using JQuery UI for the tooltip.

I've read the documentation, but haven't figured out how to program the right selector and display the custom data.

Any ideas of what I'm missing?

http://jsfiddle.net/QsEJk/2/

HTML:

<span class="info-icon"
    data-title="custom title"
    data-desc="custom description">
</span>

JS:

$( '.info-icon' ).tooltip({
    content: function() {
        return 'Title: ' + $( this ).data('title') +
               ' Description: ' + $( this ).data('desc');
    }
});
like image 622
Will M Avatar asked Jul 05 '13 19:07

Will M


2 Answers

You need the items option

$(".info-icon").tooltip({
    items: "[data-title], [data-desc]",
    content: function () {
        return 'Title: ' + $(this).data("title") + ' Description: ' + $(this).data('desc');
    }
});

http://api.jqueryui.com/tooltip/

Edit:

[data-title],[data-desc] will work if either attribute is on the .info-icon span.

[data-title][data-desc] will require both attributes specified for the tooltip to work.

like image 71
Jasen Avatar answered Nov 15 '22 09:11

Jasen


The accepted answer worked for me but in my case I wanted this applied to many items on the page so didn't want a variable for every one. Instead I used this:

        $(".help").each(function() {
            $(this).tooltip({
                content: $(this).data('help')
            });
        });

This applies the data-help content to every .help item on the page.

like image 28
Abby Avatar answered Nov 15 '22 08:11

Abby