0

I'm using JQuery UI's tooltips for a page using dynamic elements. It works perfectly on a single element, however when I attempt to add multiple tooltips the last tooltip defined seems to overwrite the existing items.

e.g.

$("body").tooltip({
    selector: ".testItem1",
    items: ".testItem1",
    content: "1"
});

$("body").tooltip({
    selector: ".testItem2",
    items: ".testItem2",
    content: "2"
});

In this case the second tooltip seems to overwrite the first.

Example (only the second items tooltip will work): https://jsfiddle.net/wce547w7/

Is there an alternative way to add tooltips to dynamic elements? Or can I use a property of some type to allow additional tooltips?

Edit: The dynamic elements are added outside of my control, so I cannot add the tooltip at the same time as creating the element.

0

1 Answer 1

1

Why use many tooltips?

// Add elements
var count = 0;
$(".addItem").on("click", function(){
    count++;
    $("body").append('<div class="testItem">Test ' + count + '</div>');
})

// Add tooltips
$("body").tooltip({
    selector: ".testItem",
    items: ".testItem",
    content: function() {return $(this).html();}

});
Sign up to request clarification or add additional context in comments.

3 Comments

The example was just to show the effect, in the actual problem the tooltip isn't just the index of the element.
That's an interesting idea, I'll swap the html out for a data attribute and give it a try.
Works perfectly, I didn't even think about doing it that way. Thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.