Java Reference
In-Depth Information
figure 17-2
This code retrieves the element with an
id
of
myDiv
, and you can use this object just like you use
any other element object. For example, the following code alerts the element's tag name with the
tagName
property:
alert(el.tagName);
You can also extend an element by passing the element's object to the dollar function. The following
code passes the
document.body
object to the dollar function in order to extend it:
var body = $(document.body);
By doing this, you can use both native DOM methods and properties as well as Prototype's methods.
Note
Prototype's dollar function returns
null
if the specified element cannot
be found. This is unlike jQuery's
$()
function because Prototype returns an
extended DOM element object; even though it is extended, it is still a DOM
element object.
Selecting elements with CSS Selectors
As mentioned before, Prototype's dollar function does not select elements based upon CSS selectors;
it only accepts element
id
values and element objects. Prototype does, however, have another
function that behaves similarly to the jQuery function: the
$$()
function.
You can use the
$$()
function to locate and retrieve elements that match the provided CSS selector. For
example, the following code retrieves all
<div/>
elements in the page and returns them in an array:
var divEls = $$("div");
The
$$()
function always returns an array—even if you use an
id
selector. One downside to
$$()
is that it returns an array of extended elements; so if you want to perform an operation on every