Jack Jack - 1 year ago 74
Javascript Question

Get element by tag name shorthand?

I don't know what it's called, but I know that there's a way to get elements based on their tags without

. 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
, but with other elements, like
. Seeing as
isn't the same as the
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.

Answer Source

As 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 completely 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.


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)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download