Rectangle 27 0

javascript Get element by tag name shorthand?


HTMLCollection
anchors
document.frames[x]
document.images[x]
images, applets, links, forms
window.frames[x]

@Pointy - JavaScript is ECMAScript in a browser interacting with the DOM. The word has become a generic term with various forms of captialisation. Node.js describes itself as "Evented I/O for V8 JavaScript", where V8 is Google's "open source JavaScript engine". Where does it say javascript "doesn't have anything to do with the DOM"?

@RobG Also, was that a downvote?

@RobG No, the standardized version of JavaScript is named ECMAScript but to most people, it's all JavaScript. Saying that it's all about the DOM doesn't change the fact that these are DOM collections and not part of the JavaScript language - if you were using JS in a different environment, you might not see these. And if you were using some other language binding for the DOM, you would still have access to these named collections. So yes, I feel justified in making the distinction between the DOM and JavaScript and saying that this is not JavaScript.

@RobG Node.js would like to have a word with you ...

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.

JavaScript is all about the DOM. The language is ECMAScript, using it with a DOM is javascript (or Javascript or JavaScript, whatever).

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 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)

Note
Rectangle 27 0

javascript Get element by tag name shorthand?


document.tag = document.getElementsByTagName;
function tag(name) {
    return document.getElementsByTagName(name);
}

(mozilla only, and doesn't work with the DOM yet)

The only APIs I know of that support querying by element name are,

or a wrapper,

Note
Rectangle 27 0

javascript Get element by tag name shorthand?


document
getElementsByTagName
var $ = document.getElementsByTagName;

// you can then use it like so:
$('SPAN').// and so on
var $ = function () { return document.getElementByTagName.apply(document, arguments); }

Agreed, I am merely pointing out how trivial it would be to create a mapping of this nature.

And let's all agree to standardise on $ being getElementById, right?

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:

Other than this, there is no built-in array-like access to all of the tags in the document:

While I was unaware that could be done and it's a nifty bit a knowledge to have, I still am curious about how many of these built-in alternatives exist. Two already exist that I know of, as mentioned in my question. I'd like more information on that rather than alternatives.

mplungian - There is no such "standarisation". In probably the most used javascript library, $ is not getElementById, it is an overloaded identifier that does many things, one of which is to return an object with numeric properties referencing elements matching a supplied selector. It has many other uses and hence meanings.

Note