I don't know what it's called, but I know that there's a way to get elements based on their tags without getElementsByTagName
. It returns the same thing, but it's shorter and I'm using tags a lot in my project. What I'm talking about is document.frames[x]
and document.images[x]
, but with other elements, like document.b[x]
or document.a[x]
. Seeing as document.images
isn't the same as the <img>
tag, it seems like if there are more they'd be named differently as well. Would anyone happen to know what it's called when using this method and/or have a list of accepted tags? Thanks.
P.S. Please do not suggest using a library such as jQuery. This project is meant to be a learning experience, so I want to use regular JavaScript.
I don't know what it's called, but I know that there's a way to get elements based on their tags without getElementsByTagName
. It returns the same thing, but it's shorter and I'm using tags a lot in my project. What I'm talking about is document.frames[x]
and document.images[x]
, but with other elements, like document.b[x]
or document.a[x]
. Seeing as document.images
isn't the same as the <img>
tag, it seems like if there are more they'd be named differently as well. Would anyone happen to know what it's called when using this method and/or have a list of accepted tags? Thanks.
P.S. Please do not suggest using a library such as jQuery. This project is meant to be a learning experience, so I want to use regular JavaScript.
Share Improve this question edited Sep 23, 2012 at 6:12 Santosh Kumar 27.9k21 gold badges70 silver badges123 bronze badges asked Apr 24, 2011 at 4:13 JackJack 651 gold badge1 silver badge4 bronze badges 4-
2
Why not just create a shorter alias to
document.getElementsByTagName
? – Cristian Sanchez Commented Apr 24, 2011 at 4:16 -
Where did you find this shorter way? Are you thinking of
document.getElementByID
? – Gordon Seidoh Worley Commented Apr 24, 2011 at 4:21 - I know how to make a function and make it shorter, but as stated the project is to help me learn as much about JavaScript as I can and this is something that I know exists but don't fully understand, nor do I know any guides and therefor I ask here for someone who may know about it. The shorter way isn't getElement, it's just like the two examples included in my question. – Jack Commented Apr 24, 2011 at 4:33
- 1 What you're talking about is not "regular JavaScript", but "things that browsers provide to the JavaScript runtime." – Pointy Commented Apr 24, 2011 at 4:39
3 Answers
Reset to default 6As mentioned elsewhere in the answers, this doesn't have anything to do with JavaScript really, these are DOM properties and methods accessible via the JavaScript language binding for the DOM.
With reference to addressing elements such as document.frames[x]
(note that this is incorrect, it should be window.frames[x]
) and document.images[x]
- these are Document Object/HTML Collections and the W3C standard includes only images, applets, links, forms
and anchors
.
So unless I'm looking in pletely the wrong place, from what I can tell from the DOM-1 and DOM-2 specs, there doesn't seem to any way of arbitrarily addressing elements by tag name the way that you remember doing.
Update
The MDC entry on HTMLCollection
is more understandable; it reads
The following lists each item (and its specific properties) which return an HTMLCollection: Document (images, applets, links, forms, anchors); form (elements); map (areas); table (rows, tBodies); tableSection (rows); row (cells)
Other than other JavaScript libraries creating these shorthands, I am not aware of any that are built into the language. It would be trivial to map this to your own shorthand:
var $ = document.getElementsByTagName;
// you can then use it like so:
$('SPAN').// and so on
Other than this, there is no built-in array-like access to all of the tags in the document:
http://www.javascriptkit./jsref/document.shtml
Create your own reference,
document.tag = document.getElementsByTagName;
or a wrapper,
function tag(name) {
return document.getElementsByTagName(name);
}
The only APIs I know of that support querying by element name are,
DOM
getElementsByTagName
CSS Selectors
querySelectorAll
XPath
evaluate
E4X
(mozilla only, and doesn't work with the DOM yet)