I’ve been dabbling with Dojo quite a bit lately. I feel great about my MooTools and jQuery skills but I’m a bit still raw when it comes to Dojo. What’s important that I keep in mind, however, is that the tasks I’m trying to accomplish are the same — the syntax is simply different. Here are a few basic JavaScript tasks and the syntax to accomplish them within each awesome framework.
Execute Code when the DOM is Ready
Dojo Toolkit
dojo.ready(function() { //do stuff });
jQuery
jQuery(document).ready(function() { //do stuff });
MooTools
window.addEvent('domready',function() { //do stuff });
Collect Elements
Dojo Toolkit
var myElement = dojo.byId('myElement'); var divs = dojo.query('div');
jQuery
var myElement = jQuery('#myElement'); var divs = jQuery('div');
MooTools
var myElement = document.id('myElement'); var divs = $$('div');
Create Event Listeners
Dojo Toolkit
dojo.connect(dojo.byId('myElement'),'onclick',function() { alert('You clicked me!'); });
jQuery
jQuery('#myElement').click(function() { alert('You clicked me!'); });
MooTools
document.id('myElement').addEvent('click',function() { alert('You clicked me!'); });
Create a New Element, Inject Into Document.Body
Dojo Toolkit
dojo.create('div',{ innerHTML: 'This is a new element', id: 'myElement' },dojo.body());
jQuery
jQuery('<div id="myElement">This is a new element</div>').appendTo('body');
MooTools
new Element('div',{ id: 'myElement', text: 'This is a new element' }).inject(document.body);
Execute AJAX Requests
Dojo Toolkit
dojo.xhrPost({ url: 'save.php', content: { id: dojo.byId('user-id').value } load: function(response) { alert('Received the following response: ' + response); } });
jQuery
jQuery.ajax({ url: 'save.php', type: 'post', data: { id: jQuery('#user-id').val() }, success: function(response) { alert('Received the following response: ' + response); } });
MooTools
new Request({ url: 'save.php', method: 'post', data: { id: document.id('user-id').value }, onSuccess: function(response) { alert('Received the following response: ' + response); } }).send();
Animate the Opacity of an Element
Dojo Toolkit
dojo.anim('myElement',{ opacity: 0.7 }, 250);
jQuery
jQuery('#myElement').fadeTo(250,0.7); //duration first...ftl
MooTools
document.id('myElement').set('tween',{ duration: 250 }).fade(0.7);
See? Dojo, jQuery, and MooTools aren’t that different. Same problems, different solution syntax. Like Pete Higgins says: It’s just JavaScript!
Read the full article at: Accomplishing Common Tasks Using MooTools, jQuery, and Dojo
Hosted by Media Temple, domain from Name.com.