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
2008-09-19 12:47:08
+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
2008-10-02 08:23:30
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
2008-10-06 06:42:43
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
2008-10-10 16:59:47