The Dollar Function

Have you ever used document.getElementById('id')? You know that one function that gets an Element from the DOM with an ID? In MooTools, there is a shortcut for that very function, and it does something extra.

var myElement = $('id');

Discussion

The dollar function, or $, is a shortcut to document.getElementById. It also adds methods to the element you call it on. (That doesn't mean much right now, but keep it in mind.)

  • A shortcut for document.getElementById.
  • Extends Element(s) to have the MooTools Element Methods.
  • Universal. The leading popular JavaScript Libraries, such as jQuery and Prototype both use $ for shortcuts to document.getElementById.

The Dollar Function Syntax

/*
  The Dollar Function Syntax
*/
var myElement = $('title');
myElement.set('html', "Changed!");

The example above sets the html of the element with the ID of title. The Syntax is very simple for this simple function. You can use it directly, or set a variable to it as in the syntax example. In MooTools, you use this often when working with things that you know exist either by design or elements you made.

Documentation Sources

You can read the MDC for getElementById.

You can also read the Mootorial for $.

Continue to The Dollars Function

 
walkthrough-1.2/the-dollar-function.txt · Last modified: 2009/02/21 04:27 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki