Dojo Basics

From Wiki
Revision as of 22:51, 31 January 2011 by Scott (talk | contribs) (Created page with ' == Find DOM Nodes by ID == <source lang="javascript"> var foo = dojo.byId('foo'); </source> == Strings == <source lang="javascript"> dojo.require('dojo.string') dojo.string.tri…')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Find DOM Nodes by ID

var foo = dojo.byId('foo');

Strings

dojo.require('dojo.string')
dojo.string.trim('  hi there  ')                                        // "hi there"
dojo.string.pad(13, 5, '0')                                             // "00013"
dojo.string.substitute("${0} and ${1}", ['Jack', 'Jill'])               // "Jack and Jill"
dojo.string.substitute("${p1} and ${p2}", {p1: 'Jack', p2: 'Jill'})     // "Jack and Jill"

Arrays

dojo.indexOf([1,2,3], 3)                             // 2
dojo.every([2,4,6], function(x){return x%2 == 0})    // true
dojo.forEach([1,2,3], function(x){console.log(x)})   // logs 1, 2, 3
dojo.map([1,2,3], function(x){return x+1})           // [2,3,4]
dojo.filter([1,2,3], function(x){return x%2 == 0})   // [2]

Styles, Attributes, Classes

dojo.style(task_row, { height: '100px', border: '1px green' })     // add style attributes to a given node
dojo.style('task_row', { height: '100px', border: '1px green' })   // can give dojo a string too
dojo.attr(task_row, 'style')                                       // "height: 100px; border: 1px green;"
dojo.attr(myButton, 'value', 'You pushed me!')
dojo.hasClass(task_row, 'active')
dojo.addClass(task_row, 'active')
dojo.removeClass(task_row, 'active')
dojo.toggleClass(task_row, 'active')

Insert Nodes

dojo.place(myNewTableRow, middleRow, 'after')
dojo.place(myNewTableRow, someTable, 'last')
dojo.place(myNewListElement, someList, 4)   // newListElement will now occupy position 4 of someList