0

I'm using tooltipster to generate tooltips, but I want to set the tooltip based on a data attribute from the element it's attached to. How do I get the associated data so I can set the image url as shown below. This is what I've been trying:

$(".test").tooltipster({
  var datainfo = $(this).data("info");
  content: $('<div class="desc"><img src="img/'+datainfo+'.jpg"/></div>')
});

My HTML is basically:

<div class="test tooltip" data-info="AAA">Testing</div>
jcuenod
  • 55,835
  • 14
  • 65
  • 102
FeelRightz
  • 2,777
  • 2
  • 38
  • 73

3 Answers3

2

Your parameter is json, not a function block so you need to take your datainfo line out of there (it's javascript, not json). Maybe this will work for you:

var datainfo = $(".test").data("info");
$(".test").tooltipster({
  content: $('<div class="desc"><img src="img/'+datainfo+'.jpg"/></div>')
});

I suppose you are wanting to set a number of tooltips that show images in this case, let's assume that each one has the class "test".

$(".test").each(function(){
  $(this).tooltipster({
      content: $('<div class="desc"><img src="img/' + $(this).data("info") + '.jpg"/></div>')
  });
});
jcuenod
  • 55,835
  • 14
  • 65
  • 102
0

Your plugin may not work in this way. You are passing couple of lines of code instead of key, value pair of options in the plugin. If you want data-info value, simple solution will be to bind the mouseover event and then get that attribute value.

Example:

$(".test").on("mouseover",function(){
    var datainfo = $(this).attr("data-info");
});

Note: In case the plugin removes the data-attribute, then you might need to use another attribute for getting the src of image.

K K
  • 17,794
  • 4
  • 30
  • 39
0

Well a small trick you can try here!!

Add a classname tooltipShow [or anything] on which you want to display tooltips. Use that class to capture hover on your element like below:

$('.tooltipShow').on('hover',function(){
    $(this).tooltipster({
         content: $('<div class="desc"><img src="img/'+$(this).data('info')+'.jpg"/></div>')
    });
});
Guruprasad J Rao
  • 29,410
  • 14
  • 101
  • 200