getElementById and document.something

I believe we cannot just choose either to use getElementByID or getting the complete path by doing document.someelement.somechildelement etc…

Can we use a selectIndex by using getElementByID

can we apply a display.none by using document.someelement. etc?

If not, as I apparently believe, why not?

Those are some questions that I would like to ask, I don’t need a big answer, just some orientation points.
I believe that, the fact we cannot use both at will, must be related with the DOM structure?

Am I completely wrong?


ps - I have some questions on other posts that I need to reply to, I will do it. It just requires time to analyze, and I needed to ask this before I forget it.

Once you get a proper reference to a dom node, you can set properties on it.
<select id=“mySelect”…

var selectElement = document.getElementById("mySelect");
selectElement.selectedIndex = 2; = "none";

In general, stay away from getting references to nodes that seem to magically appear by their id or name attribute in the document object(don’t do document.someName). There’s a lot of non-standard browser differences. Well, to be honest, the entire dom is a mine field due to all the browser inconsistencies, but that’s another topic.

Thanks. :slight_smile: Again.

I’m not sure I understand your answers and that is surelly due the fact that my question was not well formed.

Well, what I was trying to oppose was not getElementById vs document.someName;

Let’s put it like this:

Can we freely choose either to use:





If you have already answered, please do it again. :slight_smile: Please. :slight_smile:

Yes you can. As both provide the same reference to an element, you can then chain on to it methods that an element accepts.