Document.getElementById('ID'). func(....) vs ID.func(...)

Asked

Viewed 319 times

12

Yesterday I came across a curious thing, I had no idea you could do it this way.

Until now I did it this way:

document.getElementById('a').innerHTML = 'CONTENT';
<div id="a"></div>

But I’ve always seen it done this way, until yesterday I noticed that:

a.innerHTML = 'CONTENT';
<div id="a"></div>

Also works.

Clearly it doesn’t lose readability, and one great advantage is that we don’t write as much (document.getElementById('...')), can then directly reference the element only with id. I imagine implicitly the function document.getElementById('...') is still being executed, but:

Why not use this way more? At least I (as I remember) had never seen.

What are the advantages/disadvantages of one compared to the other? (if any)

  • I’ve always had that question, good question...

  • Thanks @Magichat, really had never noticed before

2 answers

11


Basically it’s a visibility problem. What happens if your id is a property of the object window? I don’t know if you know, all the properties you access globally or are in window.

The question code is the same as:

window.a.innerHTML = 'CONTENT';
<div id="a"></div>

This is not standard. It’s something Internet Explorer started to do (putting DOM Ids inside the object window), many programmer started using and other browsers felt "obligated" to maintain compatibility. But you can’t count on this.

Perform this:

top.innerHTML = 'CONTENT'; //top é uma propriedade de window
<div id="top"></div>

I put in the Github for future reference.

So in most cases it will work, but in some it won’t. Either because the implementation does not accept this form, or because it will confuse with something of the object. Isn’t it better to keep a style and use only one shape that will always work? Use the shape with getElementById() which is standard, will always be supported in any DOM implementation as per the HTML/Ecmascript specification.

It was never intended to have this compact shape by accessing the ID directly. Although to tell you the truth, here’s a little opinion, I should allow it, but in the right way, no document and not in the window, and Ids are segregated into a specific property to avoid confusion with the object.

This technique is to give you a local object (I hope you are using a var or let) which is not confused with other properties.

I think they could have done something like this:

document.ids.a.innerHTML = 'CONTENT';

There are people who think that I wouldn’t even need all this and the id Alone should already solve and the programmer to turn to if there is name conflict with some property. It makes some sense. It could even have a standard shape in the language, like using #top not to be confused with top.

I actually think it’s wrong window be the object default. document is what you use most.

  • 1

    What an identifier would look like in the Object window ?

  • @Magichat do not know if I understand what you want to know. At least beyond what you are already showing in the question.

  • I mean I don’t understand, your placement in relation to the Object window... Since the identifiers to my view will not be above Document Object (actually it is my belief), and its placement made me question myself...

  • @Magichat I’m sorry, but I don’t understand what your question is.

  • Quiet, as said in the answer below, every time you define an id automatically is created a DOM access element, this put, I do not understand the relationship with the Object window

  • @Magichat edited, see if it helped anything.

  • Um,. I think now clarified, qnd refers to the Object window , you mean the properties and conflicts with these properties, but if the id does not conflict, that’s fine?

  • @Magichat is fine, as long as you use a browser, or another mechanism that uses DOM in this way, that works like this. If you take one that doesn’t work, it won’t work. It turned out to be rare to have something like this, it’s unlikely that anyone will change this behavior one day, but it’s not standard.

  • Very good, Obgado, with your example of conflict between properties/ids, I was clarified, also had idea that there could be lack of compatibility between browsers for this implementation

  • Only for a note http://answall.com/q/123098/3635

  • @Guilhermenascimento dup?

  • @mustache I think so

Show 7 more comments

3

I believe that the only reason not to use the direct id element is to avoid conflicts, as all global variables, including the ids that are stored in browsers leaving this information "crowded"for this reason is obsolete its use preferring Getelementbyid which does what it says picks up the element by its id.

Therefore, this direct id retrieval feature may or may not work depending on your browser and the way you manage these "global variables".

  • 1

    Something that has never been made official cannot be deprecated.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.