Home > Dojo, Javascript > Dynamic Dojo Tooltips

Dynamic Dojo Tooltips

June 19th, 2007

While messing with the dojo tooltip widget in one of our applications, I had the need to dynamically create a widget. Below is a sample statement of dynamically creating a tooltip widget.

dojo.widget.createWidget('tooltip', {
	caption: 'what you want the tooltip to say',
	connectId: 'target_id_to_attach_tooltip'
});

However, this is not complete because we need to attach it to the body of the document like this:

var new_tooltip = dojo.widget.createWidget('tooltip', {
	caption: 'what you want the tooltip to say',
	connectId: 'target_id_to_attach_tooltip'
});
document.body.attachChild(new_tooltip.domNode);

You’ll notice that the return value of dojo.widget.createWidget is an object and when we attach it to the body of the document, we use the domNode property of that object.

There is one more potential problem with dynamically creating these widgets. Let’s say that we have a dynamic table and each row of the table contains the target id for each tooltip. If the table gets reloaded with new data (rows have new ids), the old tooltips remain appended to the body. No big deal of you don’t mind a little trash left behind, but the problem comes into play when you load data back into that dynamic table that may have previously existed and generate a whole new set of tooltips to go along with it. Now you may have two or more of the same tooltip appended to the body that belong to the same target id. Some browsers might be fine with this and some might choke. To prevent these duplicates, we use the following code for each new tooltip widget that is created:

function createTooltip(target_id, content) {

	// prepending 'tt_' to distinguish between target and tooltip id's
	var obj = document.getElementById('tt_'+target_id);

	if(obj != null)
		obj.parentNode.removeChild(obj);

	var tooltip = dojo.widget.createWidget('tooltip', {
		caption: content,
		connectId: target_id
	});

	tooltip.domNode.id = 'tt_'+target_id;
	document.body.appendChild(tooltip.domNode);
}

You might notice that we are actually adding an ID to the tooltip. This allows us to check in the future if that tooltip has already been created. If it has, the code above will destroy it and create a new tooltip in its place. This should assist those searching for answers about dynamically creating dojo tooltip widgets.

rgillette Dojo, Javascript , , ,

  1. Bob Flavin
    June 10th, 2009 at 13:33 | #1

    In line 5 of the second set of code, I think that is ‘document.body.appendChild’ attachChild. (it is correct in the third set of code.

    I can’t find dojo.widget.createWidget in Dojo versions 1.1.1 or 1.3. I assume that something like ‘new dijit.Tooltop(…)’ would be appropriate?

    In any event, I can’t get it to work, the widget is created, a appears in the DOM, but it contains no content and doesn’t work.

  2. Bob Flavin
    June 10th, 2009 at 14:15 | #2

    titlesTip = new dijit.Tooltip({
    label: ‘what you want the tooltip to say’,
    connectId: [titlesTab.id]});

    I got the code above to work! (After lots of code reading and tracing.) Note that the connectId expects an array (the ‘[' ']‘) and I used the ‘label’ property rather than ‘caption’ There is also an ‘innerHTML’ property that seems to be similar to the ‘label’. I don’t know what versions we are working with, but incompatible changes like this in the Dojo base are pretty bad.

  1. No trackbacks yet.