The $() function The
dollar function, $(), can be used as shorthand for the
getElementById function. To refer to an element in the
Document Object Model (DOM) of an
HTML page, the usual function identifying an element is: document.getElementById("id_of_element").style.color = "#ffffff"; The $() function reduces the code to: $("id_of_element").setStyle({color: '#ffffff'}); The $() function can also receive an element as parameter and will return, as in the previous example, a prototype extended object. var domElement = document.getElementById("id_of_element"); // Usual object reference returned var prototypeEnhancedDomElement = $(domElement); // Prototype extended object reference :
Note: Like the underscore (_), the $ character is a legal "word character" in JavaScript identifiers, and has no other significance in the language. It was added to the language at the same time as support for
regular expressions, so that the
Perl-like matching variables could be emulated, such as $` and $'.
The $F() function Building on the $() function: the $F() function returns the value of the requested form element. For a 'text' input, the function will return the data contained in the element. For a 'select' input element, the function will return the currently selected value. $F("id_of_input_element")
The $$() function The
dollar dollar function is Prototype's
CSS Selector Engine. It returns all matching elements, following the same rules as a selector in a CSS stylesheet. For example, if you want to get all <a> elements with the class "pulsate", you would use the following: $$("a.pulsate") This returns a collection of elements. If you are using the script.aculo.us extension of the core Prototype library, you can apply the "pulsate" (blink) effect as follows: $$("a.pulsate").each(Effect.Pulsate);
The Ajax object In an effort to reduce the amount of code needed to run a cross-browser XMLHttpRequest function, Prototype provides the Ajax object to abstract the different browsers. It has two main methods: Ajax.Request() and Ajax.Updater(). There are two forms of the Ajax object. Ajax.Request returns the raw XML output from an AJAX call, while the Ajax.Updater will inject the return inside a specified DOM object. The Ajax.Request below finds the current values of two HTML form input elements, issues an HTTP POST request to the server with those element name/value pairs, and runs a custom function (called showResponse below) when the HTTP response is received from the server: new Ajax.Request("http://localhost/server_script", { parameters: { value1: $F("form_element_id_1"), value2: $F("form_element_id_2") }, onSuccess: showResponse, onFailure: showError }); ==Object-oriented programming==