Utility Methods:
$(id | element) -> HTMLElement
$((id | element)...) -> [HTMLElement...]
If provided with a string, returns the element in the document with matching ID;
otherwise returns the passed element. Takes in an arbitrary number of arguments.
All elements returned by the function are extended with Prototype DOM
extensions.
$$(cssRule...) -> [HTMLElement...]
Takes an arbitrary number of CSS selectors (strings) and returns a
document-order array of extended DOM elements that match any of them.
$A(iterable) -> actualArray
Accepts an array-like collection (anything with numeric indices) and returns its
equivalent as an actual Array object. This method is a convenience alias of
Array.from, but is the preferred way of casting to an Array.
$F(element) -> value
Returns the value of a form control. This is a convenience alias of
Form.Element.getValue. Refer to it for full details.
$H([obj]) -> Hash
Creates a Hash (which is synonymous to “map” or “associative array” for
our purposes). A convenience wrapper around the Hash constructor, with a
safeguard that lets you pass an existing Hash object and get it back untouched
(instead of uselessly cloning it).
$R(start, end[, exclusive = false]) -> ObjectRange
Creates a new ObjectRange object. This method is a convenience wrapper around
the ObjectRange constructor, but $R is the preferred alias.
$w(String) -> Array
Splits a string into an Array, treating all whitespace as delimiters. Equivalent
to Ruby's %w{foo bar} or Perl's qw(foo bar).
Element
Element.removeClassName(elementID,theclass);
Remove the class from the element.
Element.addClassName(elementID,theclass);
Add the class theclass to the element
new Insertion.Before(elementid,text here);
Inserts the text directly before the elementid element.
text here<span id=elementid>Original</span>
new Insertion.Top(elementid,text here);
Inserts the text inside the element at the top:
<span id=elementid>text here Original</span>
new Insertion.Bottom(elementid, text here);
Inserts the text inside the element at the bottom:
<span id=elementid>Original text here</span>
new Insertion.After(elementid,text here);
Inserts the text directly after the elementid element.
<span id=elementid>Original</span>text here
$(element1,element2).each( function (theobj){
alert(theobj.innerHTML);
});
Traverse through the array using the .each syntax similar to ruby.
Element.hide(elementid);
Hides the element
Element.show(elementid);
Shows the element
Element.toggle(elementid);
Toggles the show/hide status of an element
Element.remove(elementid);
Remove an element from the page
Form Functions
$F(fieldname);
Return the value of the form element, whether it is a text input, textarea,
select box or checkbox. If it is a checkbox, it will return undefined if
unchecked. Radio groups dont work.
Form.getElements(formID);
Returns an array of all the form elements for form formID
Form.serialize(formID);
Returns a formatted URL containing all the elements in the form, similar to
&field=value&field2=othervalue
Form.focusFirstElement(formID);
Will set focus on the first form element.
Exception Handling
Try.these(
function(){
// errors
},
function(){
// other stuff
}
);
Allows you to execute the second function if the first one fails.
Kinda like try/catch, except it doesn't make any sense.
Ajax
function ajaxMe( theUrl, data ){
var ajaxRequest = new Ajax.Request(
theUrl,{method: post, parameters: data, onComplete: theResponse});
}
function theResponse(origRequest){
alert(origRequest.responseText);
}
Classes
var Butter = Class.create({
initialize: function(brand) {
this.brand = brand;
this.melted = false;
},
melt: function() {
this.melted = true;
}
})
var parkay = new Butter('Parkay');
Prototype gives you a way to create classes. If you want, you
can define an initialize function that will be called when
instances of the class are created.