0

I have this function for tooltips:

//Tooltips
(function($) {
    $.fn.tooltips = function(html) {
        var $tooltip,
            $html;
        return this.each(function(data, html) {
            $html = $(html).attr("data-tooltip", data);
            var $tooltip = $('<div class="tooltip" data-tooltip="' + data + '">' + $html.attr('title') + '</div>').appendTo(".tip");
            $html.removeAttr("title").hover(function() {
                $html = $(this);
                $tooltip = $('div[data-tooltip=' + $html.data('tooltip') + ']');
                $tooltip.addClass("active");
            }, function() {
                $html = $(this);
                $tooltip = $('div[data-tooltip=' + $html.data('tooltip') + ']');
                setTimeout(function() {
                    $tooltip.removeClass("active");
                }, 100);
            });
        });
    }
})(jQuery);

$(function() {
    $("a.tip[title]").tooltips();
});

And I have a PHP loop with this:

echo '<span class="author-meta"><a class="author tip" id="'. $username .'" href="#" title="[Removed]">'. $username .'</a></span><br>';

Currently when you hover over one name, all the title tags appear on all the names. Is there an easy method to have it so if you hover over one name - Only that name has the hover event?

The code is from a free tutorial website that I can't seem to find anymore but I don't remember this particular issue being mentioned in the comment section so I'm at a loss!

2

1 Answer 1

1

The mistake in your code is in:

.appendTo(".tip")

That is appending the tooltip to all the elements with class="tip". You should only append to the current element. The code in line 8 should be:

var $tooltip = $('<div class="tooltip" data-tooltip="' + data + '">' + $html.attr('title') + '</div>').appendTo(this);
Sign up to request clarification or add additional context in comments.

1 Comment

Aha! So simple! Thank you!

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.