Skip to content

Instantly share code, notes, and snippets.

@amejiarosario
Last active May 27, 2016 15:52
Show Gist options
  • Save amejiarosario/366bd1c28b4c7535e610b49b6ec0b712 to your computer and use it in GitHub Desktop.
Save amejiarosario/366bd1c28b4c7535e610b49b6ec0b712 to your computer and use it in GitHub Desktop.
80/20 Dev Cheatsheets

Document Object Model (DOM)

Window

Document

Only the most used. Full list

document.body

return content. image

document.title

get/set title of the page

document.location

gets URI of current page image

Document.getElementsByClassName()

get a list of elements with the given class name.

Document.getElementsByTagName()

get a list of elements with the given tag name.

document.getElementById(String id)

get an object reference to the identified element

Document.createElement()

Creates a new element with the given tag name.

  // create a new div element 
  // and give it some content 
  var newDiv = document.createElement("div"); 
  var newContent = document.createTextNode("Hi there and greetings!"); 
  newDiv.appendChild(newContent); //add the text node to the newly created div. 

  // add the newly created element and its content into the DOM 
  var currentDiv = document.getElementById("div1"); 
  document.body.insertBefore(newDiv, currentDiv); 

document.querySelector(String CSS selector)

get the first Element node within the document, in document order, that matches the specified selectors.

document.querySelectorAll(String CSS Selector)

Returns a list of all the Element nodes within the document that match the specified selectors. CSS Selectors

document.querySelectorAll("a[class*='item']");
document.querySelector("div.user-panel.main input[name=login]");

Document.createAttribute()

Creates a new Attr object and returns it.

var node = document.getElementById("div1");
var a = document.createAttribute("my_attrib");
a.value = "newVal";
node.setAttributeNode(a);
console.log(node.getAttribute("my_attrib")); // "newVal"

Console

console.log() - image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment