Apr
25
2010
25
2010
Accomplishing Common Tasks Using MooTools, jQuery, and Dojo
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
});
Click here to copy this code to the clipboardClick here to add this snippet to CodaClick here to add this snippet to TextMateGet the raw code
1dojo.ready(function() {
2 //do stuff
3});
jQuery
$(document).ready(function() {
//do stuff
});
Click here to copy this code to the clipboardClick here to add this snippet to CodaClick here to add this snippet to TextMateGet the raw code
1$(document).ready(function() {
2 //do stuff
3});
MooTools
window.addEvent('domready',function() {
//do stuff
});
Click here to copy this code to the clipboardClick here to add this snippet to CodaClick here to add this snippet to TextMateGet the raw code
1window.addEvent(‘domready’,function() {
2 //do stuff
3});
Collect Elements
Dojo Toolkit
var myElement = dojo.byId('myElement');
var divs = dojo.query('div');
Click here to copy this code to the clipboardClick here to add this snippet to CodaClick here to add this snippet to TextMateGet the raw code
1var myElement = dojo.byId(‘myElement’);
2var divs = dojo.query(‘div’);
jQuery
var myElement = jQuery('#myElement');
var divs = jQuery('div');
Click here to copy this code to the clipboardClick here to add this snippet to CodaClick here to add this snippet to TextMateGet the raw code
1var myElement = jQuery(‘#myElement’);
2var divs = jQuery(‘div’);
MooTools
var myElement = document.id('myElement');
var divs = $$('div');
Click here to copy this code to the clipboardClick here to add this snippet to CodaClick here to add this snippet to TextMateGet the raw code
1var myElement = document.id(‘myElement’);
2var divs = $$(‘div’);
Create Event Listeners
Dojo Toolkit
dojo.connect(dojo.byId('myElement'),'onclick',function() {
alert('You clicked me!');
});
Click here to copy this code to the clipboardClick here to add this snippet to CodaClick here to add this snippet to TextMateGet the raw code
1dojo.connect(dojo.byId(‘myElement’),’onclick’,function() {
2 alert(‘You clicked me!’);
3});
jQuery
jQuery('#myElement').click(function() {
alert('You clicked me!');
});
Click here to copy this code to the clipboardClick here to add this snippet to CodaClick here to add this snippet to TextMateGet the raw code
1jQuery(‘#myElement’).click(function() {
2 alert(‘You clicked me!’);
3});
MooTools
document.id('myElement').addEvent('click',function() {
alert('You clicked me!');
});
Click here to copy this code to the clipboardClick here to add this snippet to CodaClick here to add this snippet to TextMateGet the raw code
1document.id(‘myElement’).addEvent(‘click’,function() {
2 alert(‘You clicked me!’);
3});
Create a New Element, Inject Into Document.Body
Dojo Toolkit
dojo.create('div',{
innerHTML: 'This is a new element',
id: 'myElement'
},dojo.body());
Click here to copy this code to the clipboardClick here to add this snippet to CodaClick here to add this snippet to TextMateGet the raw code
1dojo.create(‘div’,{
2 innerHTML: ‘This is a new element’,
3 id: ‘myElement’
4},dojo.body());
jQuery
$('<div id="myElement">This is a new element</div>').appendTo('body');
Click here to copy this code to the clipboardClick here to add this snippet to CodaClick here to add this snippet to TextMateGet the raw code
1$(‘<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);
Click here to copy this code to the clipboardClick here to add this snippet to CodaClick here to add this snippet to TextMateGet the raw code
1new Element(‘div’,{
2 id: ‘myElement’,
3 text: ‘This is a new element’
4}).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);
}
});
Click here to copy this code to the clipboardClick here to add this snippet to CodaClick here to add this snippet to TextMateGet the raw code
1dojo.xhrPost({
2 url: ‘save.php’,
3 content: {
4 id: dojo.byId(‘user-id’).value
5 }
6 load: function(response) {
7 alert(‘Received the following response: ‘ + response);
8 }
9});
jQuery
jQuery.ajax({
url: 'save.php',
type: 'post',
data: {
id: jQuery('#user-id').val()
},
success: function(response) {
alert('Received the following response: ' + response);
}
});
Click here to copy this code to the clipboardClick here to add this snippet to CodaClick here to add this snippet to TextMateGet the raw code
1jQuery.ajax({
2 url: ‘save.php’,
3 type: ‘post’,
4 data: {
5 id: jQuery(‘#user-id’).val()
6 },
7 success: function(response) {
8 alert(‘Received the following response: ‘ + response);
9 }
10});
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();
Click here to copy this code to the clipboardClick here to add this snippet to CodaClick here to add this snippet to TextMateGet the raw code
1new Request({
2 url: ‘save.php’,
3 method: ‘post’,
4 data: {
5 id: document.id(‘user-id’).value
6 },
7 onSuccess: function(response) {
8 alert(‘Received the following response: ‘ + response);
9 }
10}).send();
Animate the Opacity of an Element
Dojo Toolkit
dojo.anim('myElement',{ opacity: 0.7 }, 250);
Click here to copy this code to the clipboardClick here to add this snippet to CodaClick here to add this snippet to TextMateGet the raw code
1dojo.anim(‘myElement’,{ opacity: 0.7 }, 250);
jQuery
jQuery('#myElement').fadeTo(250,0.7);
//duration first...ftl
Click here to copy this code to the clipboardClick here to add this snippet to CodaClick here to add this snippet to TextMateGet the raw code
1jQuery(‘#myElement’).fadeTo(250,0.7);
2//duration first…ftl
MooTools
document.id('myElement').set('tween',{ duration: 250 }).fade(0.7);
Click here to copy this code to the clipboardClick here to add this snippet to CodaClick here to add this snippet to TextMateGet the raw code
1document.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!
Related Posts
Leave a comment
Tags
ajax
Best of
Blogger
business card
Button
Calender
Clone
Content Slider
CSS
CSS Layout
demo
Design
Design Package
Facebook
Font
Framework
Google
graphic design
Htaccess
HTML
JavaScript
jQuery
Logo
mobile site
MooTools
Music Player
MySQL
News Scroller
Open Source
Photoshop
php
plugin
PSD
script
SEO
Slideshow
Social Network
Template
theme
ToolTip
tutorial
Video Player
Wallpaper
wordpress
WYSIWYG

An article by qeqnes







