Consider the following code:
<html>
<head></head>
<body>
<div id='test' class='blah'>
<a href='http://somesite.com/' id='someLink'>click!</a>
</div>
</body>
</html>
So I just recently discovered that this creates a javascript object called someLink
and I can for instance get the value of the href attribute with someLink.href
. I tested this in the latest Chrome, FF and IE and it works.
First off, how long has this "feature" been around? I imagine probably a while, because I have known for years that IDs for html elements on a page must be unique, and if you do have more than one element sharing the same ID, the last one overwrites the previous one(s), and using for instance getElementById() will return the last one. But I never really understood why, but now, looking at it as a "this is creating an object" perspective, it makes sense. So, as far as being able to directly access it with the id-name-as-javascript object...how long has that been around? IE6 era? Earlier?
2nd...I guess this is more of a discussion point than question, but... IMO this doesn't seem like a very good "feature" to have... Isn't the whole point of having a DOM and wrapper functions like getElementById()
, to give some organization and more importantly, cut down on namespace issues? I don't feel I should have to be worried about random html elements on a page overwriting my javascript variables (something that has recently happened, which is why I discovered this "feature"). Does anybody know why this is as it is, what's the logic behind it?