Skip to content

Instantly share code, notes, and snippets.

@jafow
Created October 31, 2016 04:19
Show Gist options
  • Save jafow/7582eecce72eb8dda2950276ed8c1da4 to your computer and use it in GitHub Desktop.
Save jafow/7582eecce72eb8dda2950276ed8c1da4 to your computer and use it in GitHub Desktop.
Framework - get elements on the DOM
// get elements by id:
document.getElementById('logout-button');
// returns single element
// => <button id='logout-button'>Logout!</button>
// get elements by class name:
document.getElementsByClassName('order-items');
// returns HTML collection of elements
// => [<li class="order-items">JavScripts</li>, <li class="order-items">Codes</li>, <li class="order-items">Coffees</li>]
// get elements by their relationship to other elements:
var logout = document.getElementById('logout-button');
logout.parentElement
// returns the element that the logout button is nested within, i.e,
// <div class="logout-container">(... other elements in here including logout btn)</div>
var navbar = document.getElementById('nav');
navbar.children
// returns an HTML collection of the child elements
navbar.children[0]
// accesses the first child element of navbar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment