views:

2283

answers:

4

How do I get an element or elementlist by it's tag name. Take for example that I want all elements from

+5  A: 

document.getElementsByTagName('a') returns an array. Look here for more information: http://developer.mozilla.org/En/DOM/Element.getElementsByTagName

Matthias Kestenholz
+2  A: 

Use $$() and pass in a CSS selector.

Read the Prototype API documentation for $$()

This gives you more power beyond just tag names. You can select by class, parent/child relationships, etc. It supports more CSS selectors than the common browser can be expected to.

nertzy
A: 

Matthias Kestenholz:

getElementsByTagName returns a NodeList object, which is array-like but is not an array, it's a live list.

var test = document.getElementsByTagName('a');
alert(test.length); // n
document.body.appendChild(document.createElement('a'));
alert(test.length); // n + 1
eyelidlessness
A: 

If you use getElementsByTagName, you'll need to wrap it in $A() to return an Array. However, you can simply do $$('a') as nertzy suggested.

Caged