Skip to content

Instantly share code, notes, and snippets.

@SanjeQi
Last active December 14, 2018 12:09
Show Gist options
  • Save SanjeQi/c859d8547fc55a4d1abf5a48af455150 to your computer and use it in GitHub Desktop.
Save SanjeQi/c859d8547fc55a4d1abf5a48af455150 to your computer and use it in GitHub Desktop.
document.all;
// returns all the nodes inside the document object
document.contentType;
// returns the type of content contained. Most web pages should return "text/html"
document.URL;
// returns the URL of the document object
----------------------------------------------
document.getElementById()
//This method provides the quickest access to a node, but it requires that we know something
//very specific about it — its id. Since IDs must be unique, this method only returns
//one element.(If you have two elements with the same ID, this method returns
//the first one — keep your IDs unique!)
<div>
<h5 id="greeting">Hello!</h5>
</div>
//we could find the h5 element with
document.getElementById('greeting')
//Notice how the id that we pass to getElementById is identical to the id in <h5 id="greeting">
------------------------------------------------
document.getElementsByClassName()
//This method, as its name implies, finds elements by their className. Unlike id,
//className does not need to be unique; as //such, this method returns an
//HTMLCollection (basically a list of DOM nodes — note that it is not an array, even though it
//has a length property) of all the elements with the given class. You can iterate over an HTMLCollection
//with a simple for loop.
<!-- the `className` attribute is called `class` in HTML -- it is a bummer -->
<div>
<div class="banner">
<h1>Hello!</h1>
</div>
<div class="banner">
<h1>Sup?</h1>
</div>
<div class="banner">
<h5>Tinier heading</h5>
</div>
</div>
//we could find all of the elements with className === 'banner' by calling
document.getElementsByClassName('banner')
---------------------------------------------------------------------
document.getElementsByTagName()
//Suppose you dont know an elements ID but you do know its tag name (the tag name is
//the main thing between the <>, e.g., 'div', 'span', 'h1', etc.). Since tag names arent unique, this method returns an //HTMLCollection of 0 to many nodes with the given tag.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment