Dynamically Changing dijit.TooltipDialog

I've spent the last week trying to use dijit.TooltipDialog to display widgets programmatically, and although I've found many examples of how to do this, none really addressed my particular case. In this blog entry I will provide a working example of how to manipulate a TooltipDialog using code.

Start Simple
Let's just create a dialog with a textbox on it, along with a simple onclick trigger:

<div onclick="showDialog()">clickme</div>   
<br>
<br>
<div id="dialogTarget">dialog appears here</div> 

<script type="text/javascript">
dojo.require("dijit.form.TextBox");
dojo.require("dijit.TooltipDialog");

var dialog;

dojo.addOnLoad(function(){
var tb = new dijit.form.TextBox({name:"tb1"});

dialog = new dijit.TooltipDialog({
content: tb,
onBlur: function() { dijit.popup.close(dialog); }
});
});

function showDialog(){
dijit.popup.open({ popup: dialog, around: dojo.byId("dialogTarget") });
}

</script>

A few things to note about this code - the content of a tooltip can be a dijit widget or HTML in quotes, and we can make the toolip appear on any element of our choice using the dijit.popup function. Most examples I could find showed content as HTML and the display of the tooltip triggered by a dijit.form.DropDownButton, neither of which fit my needs.

Change the Content
So what happens if the content of the tooltip dialog needs to be changed at runtime? This can be addressed using the attr() function:
<div onclick="showDialog(false)">clickme</div>   
<div onclick="showDialog(true)">clickme again</div>
<br>
<br>
<div id="dialogTarget">dialog appears here</div> 

<script type="text/javascript">
dojo.require("dijit.form.TextBox");
dojo.require("dijit.form.NumberSpinner");
dojo.require("dijit.TooltipDialog");

var dialog;

dojo.addOnLoad(function(){
var tb = new dijit.form.TextBox({name:"tb1"});

dialog = new dijit.TooltipDialog({
content: tb,
onBlur: function() { dijit.popup.close(dialog); }
});
});

function showDialog(changeContents){

if (changeContents){
var mySpinner = new dijit.form.NumberSpinner({
value: 1000,
smallDelta: 10,
constraints: {
min: 9,
max: 1550,
places: 0
},
style: "width:100px"
});

dialog.attr("content", mySpinner);
}

dijit.popup.open({ popup: dialog, around: dojo.byId("dialogTarget") });
}
</script>
In this code I've made a few changes to support two options - clicking on one element (clickme) will show the dialog we created in the first example, and another (clickme again) will change the contents of the dialog with a dijit.form.NumberSpinner. The key line of code here is dialog.attr("content", mySpinner).

Try it here.


Comments

Popular posts from this blog

How to Open QR Codes

Jim Henson predicted the end of the world

Mega Giveaway! (Today only!!!)