views:

39

answers:

2

Today a workmate was working on a third-party video player integration for a set-top-box and found in their documentation an example like this:

<a id="say_hello">Hello</a>

<script type="text/javascript">
  say_hello.onclick = function() { alert("Hello world") }
</script>

I just suspected it was something related only with CE-HTML but tried it on Google Chrome and worked aswell.

Since when is it possible to access DOM elements this way?

A: 

Since at least Javascript 1.2, I believe. I've been using this kind of thing for about as long as I've been doing front-end programming.

Robusto
so... it does work on every browser?
knoopx
@knoopx: It works on all modern browsers. I should point out that you might want to learn how to use `addEventListener()` (in IE, `attachEvent` ... or just use a framework like jQuery) because directly assigning event handlers to listeners can blow away listeners that may already exist.
Robusto
Whoa, whoa, whoa, whoa, whoa, whoa, WHOA.... This is not part of "Javascript 1.2", whatever that means. This is a DOM thing, and is **not endorsed by any standard**. Firefox for example does not (and I hope will never) support this. `document.getElementById` exists for a reason.
Crescent Fresh
@Crescent Fresh: You're right. I answered thinking the question was about assigning an event handler. I missed the part where he addressed the DOM element via direct reference, probably because I've been doing a lot of Flex lately where that is allowed. It didn't look odd to my eyes. But, yeah, `getElementById()` is definitely the way to go.
Robusto
A: 

Read the Shortcut Accessors section in this link: http://jibbering.com/faq/notes/form-access/. I think what you're accessing are the named properties. I even tried putting the <a> in a <div> but still I was able to access it using its name as above.

Sidharth Panwar