briancray / tooltipsy

Introducing a jQuery tooltip plugin for control-obsessed designers.
http://tooltipsy.com
MIT License
325 stars 97 forks source link

provide the current element to the content function #9

Closed eikes closed 13 years ago

eikes commented 13 years ago

provide the current element to the content function to allow it to access its "data-" attributes and traverse the DOM from there to find content etc.

briancray commented 13 years ago

It can be, as shown on the AJAX sample on this site.

content: function () {
        var $el = $(this);
        $.get('api.php', function (data) {
            $el.html(data).show();
        });
        return 'Fallback content';
    }
eikes commented 13 years ago

What you are showing is how "this" is bound to the tooltipsy element itself but there is no way to access the node which triggered the tooltip.

My extension allows the following:

<span class="a">Oh hai!</span>
<span style="display: none">
 <span class="c">
    More <a href="http://example.com">Info</a>!
  </span>
</span>
<script>
  $('.a').tooltipsy({
    content: function(el) {
     return $(el).next().find(".c");
  });
</script>

This is very useful as you can embed html to display in the tooltip next to the element that will show it.

briancray commented 13 years ago

still okay

var $el = $(this);
$el.data('rootel').whatever();
eikes commented 13 years ago

That can't work, because this.$tip.data('rootel', this.$el); is set after the content function is called, which causes rootel to be undefined at the time.

It works though if the last two lines in readify are changed to:

this.$tip = $('<div class="' + this.settings.className + '">').appendTo(this.$tipsy);
this.$tip.data('rootel', this.$el);
this.$tip.html(this.settings.content != '' ? this.settings.content(this.$el) : this.title);

I can open another pull request if you like.

briancray commented 13 years ago

great eye!

If that's the only change in your pull request, I'll pull.

Thanks again!