One way to go about getting a reference to an element is by way of an elements id attribute. The typical way to go about getting a reference to an element by way of an id attribute would be to use the document.getElementById method of the document object. There are other ways of getting a reference to an element by id, and of course many other ways to get references to elements by other aspects of an element, but this is one of the more tired yet true ways to do so that have been around for a great while now.
The use of the method is simple enough, just call the method off of the document object, and pass a string that is the id of the element that you want as the first and only argument. If all goes well the value that is returned should be a reference to the desired element with the set id attribute if such an element is there in the HTML.
Works fine assuming that the desired element has an id, and you know what it is. No worries there is of course a great deal of other ways to go about grabbing an element when you do not have one. Also when you do have one there are lots of ways to go about getting references to other child and parent elements. In this example you might have noticed that I am using the children HTML collection of the reference to the div as a way to access another reference that is a child of the one that I obtained by way of the document.getElelementById method.
When making a complex project in which document.getElementById is going to be called many times there might be a desire to wrap the method in another method that will just serve as a more concise way of doing the same thing. There is not much wrong with just repeating the lengthly method over and over again really, it is just the many would say that it looks bad.
If document.getElementById is only used once or twice there is no need, but at some point it might be a good move to do something like this. The other option is to find other ways of going about getting and working with html collections rather than just a single element. The document.getElementById method is just one tool in the toolbox after all, so maybe a better option would be to drop the use of the method all together, and get at your html code by some other means.
There is actually a thing where by just setting an id attribute for an element, that results in a global variable being defined that is a reference to that element. So that means that I can just use the name of the id, as a reference to the element actually.
Although this can be done it is not generally a good idea to use it as a way to go about getting a reference to an element. The window property can be over written, for example I can create a variable name foo and assign something to it other than a reference to the div element. However if I use the get element by id method, or another other method to get a reference to the div, that will always return a reference to the div.
So one of the many alternatives to getElementById is the document.getElementsByClassName method that will get a collection of elements by way of class name rather than by id. So then as the name suggests this method will return an HTMLcollection rather than just a single element reference where each of the elements in the collection has the given class name when calling the method.
An HTMLCollection is not an Array, but it is an Array like object, so Array methods can be used via Function.call and doing so will work okay ore often than not as long as it is a read only like method. You might still see this used in code examples now and then, but as of late there is more modern alternatives such as querySelectorAll that might prove to be a better option as that can be used to get by class also as well as many other ways, more on that one later.
There is also a way to get a html collection of all elements of a given tag name such as div rather than any elements that have a class name or just one element by id. This method would be the document.getElementsByTagName method that like the get elements by class name will also return an html collection of all elements that are of a given tag..
Just like document.getElementBy id this is a tired yet true method that will work in really old browsers, and still works just fine if I just want to get elements by a tag name. However when it comes to not caring so much about code breaking on old browsers any more there is again document.querySelector and document.querySelectorAll that are flexible methods that can do everything that these tired yet true methods do and more.
For example if I want to get all elements that are of a given class like the document.getElementsByClassName method for that I can call document.querySelectorAll and pass a string that begins with a dot which means I want to select by class name, and then the name of the class.
There is yet another method in the document object that can be used to get a reference to an element, and that would be the document get element from point method. As the name suggests this method can be used to get a reference to an element by way of a 2d position relative to the upper left corder of the browser window. In the event that there are two elements on top of each other than it is the top most element that is returned by this method. In the event that I want to get all elements that over lay each other in the from of a collection of element then I will need to work other another solution for that.
In the document object there are a number of properties that are references to various elements in a page. Often these are references to elements and nodes to which there should only be one instance of anyway, such as the body, head, and title elements. So the concept of getting a reference to one of these elements by way of an id attribute is kind of silly sense there will always, or at least should always be just one of them.
Everything that is a display element should ultimately be a child of the body element. There is only one of these elements in a page, and the only parent of this element should be the html element. So there is a document.body property that is a reference to this single element.
Another one of these kinds of properties is the document title property. This of course is the element that will contain the text that is found in the tab of the page in the browser. Like body this is also the kind of element to which there should be one one of them, so of course there is a reference to it in the document object.
To get a reference to the head element, just like that with the document.body property there is also a document.head property.
When it comes to getting the main html element, there is not a document html property, but there is a document element property of the document object. The html element is also often referred to as the root element, as it is the root most element when it comes to html, and there is not really anything below that, at least as far as html elements are concerned anyway.
There are also references to other elements in the element objects themselves such as the parentNode and children properties of element objects, however maybe that is all a matter for another post when it comes to parents and children from an element. As time goes by I might become aware of yet event more ways to go about getting references to elements at which point I might get around to expanding this post further.