views:

34

answers:

2

I'm trying to follow this example to add a tooltip to a flot graph. But the tooltip does not show up.

$(document).ready(function(){
    $("#plotarea").bind("plothover", function (event, pos, item) {
        $("#x").text(pos.x.toFixed(2));
        $("#y").text(pos.y.toFixed(2));
        if (item) {
          $("#charttooltip").remove();
        var x = item.datapoint[0].toFixed(2),
        y = item.datapoint[1].toFixed(2);
        showChartTooltip(item.pageX, item.pageY,'tooltip text to display');
        } else {
          $("#charttooltip").remove();
        }
    });
});

function showChartTooltip(x, y, contents) {
        $('<div id="charttooltip">' + contents + '</div>').css( {
              position: 'absolute',
              display: 'visible',
              'z-index': 100,
              top: y + 5,
              left: x + 5,
              border: '1px solid #bfbfbf',
              padding: '2px',
              'background-color': '#ffffff',
              opacity: 1                              
        }).appendTo($('#main_body')).fadeIn(200);
        } 
</script>

<div class="main_body"> //->is present in the html

I checked with firebug if the showChartTooltip method is called. This works. When I step through the code everything seems to work as expected, only I do not know who I can judge in firebug whether the appendTo call is successfull.

This was the first thing I wanted to check since the tooltip does not show up. I inserted also a very high z-index in order to be sure that this is not the problem.

If somebody has an idea what else could be wrong.. I would really appreciate your comments.

+1  A: 

Open the HTML panel in firebug and expand the body element, if a div with id=charttooltip is added then the method works. Let me know if this is the problem.

mck89
Thanks that helped me also a lot. That solved part of my problem. I was always looking at the inspect javascript tab of firebug. But I was not aware that firebug actually updates the html source also.
Tom Tom
You could also just try to select it using the console: `$('#charttooltip')` ... you'll get the element back if it was added successfully.
thenduks
+1  A: 

If the &lt;s and so on are actually present in your code then that's almost certainly your problem. You need to write actual html as the parameter:

$('<div id="charttooltip">' + contents + '</div>')
thenduks
uuhh.. yes exactly. that was the problem. thank you! I'm wondering why the author of http://www.codeunit.co.za/2009/12/30/how-to-add-a-tooltip-to-a-flot-jquery-graph/ used <
Tom Tom
Probably just his blogging software. Leave a comment and let him know!
thenduks