Created
February 1, 2019 19:19
-
-
Save amantheroot/5322f230c0efa8c505456f65195d7bce to your computer and use it in GitHub Desktop.
JavaScript DOM Manipulation cheat sheet by Traversy Media
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// EXAMINE THE DOCUMENT OBJECT // | |
// console.dir(document); | |
// console.log(document.domain); | |
// console.log(document.URL); | |
// console.log(document.title); | |
// //document.title = 123; | |
// console.log(document.doctype); | |
// console.log(document.head); | |
// console.log(document.body); | |
// console.log(document.all); | |
// console.log(document.all[10]); | |
// // document.all[10].textContent = 'Hello'; | |
// console.log(document.forms[0]); | |
// console.log(document.links); | |
// console.log(document.images); | |
// GETELEMENTBYID // | |
// console.log(document.getElementById('header-title')); | |
// var headerTitle = document.getElementById('header-title'); | |
// var header = document.getElementById('main-header'); | |
// console.log(headerTitle); | |
// headerTitle.textContent = 'Hello'; | |
// headerTitle.innerText = 'Goodbye'; | |
// console.log(headerTitle.innerText); | |
// headerTitle.innerHTML = '<h3>Hello</h3>'; | |
// header.style.borderBottom = 'solid 3px #000'; | |
// GETELEMENTSBYCLASSNAME // | |
// var items = document.getElementsByClassName('list-group-item'); | |
// console.log(items); | |
// console.log(items[1]); | |
// items[1].textContent = 'Hello 2'; | |
// items[1].style.fontWeight = 'bold'; | |
// items[1].style.backgroundColor = 'yellow'; | |
// // Gives error | |
// //items.style.backgroundColor = '#f4f4f4'; | |
// for(var i = 0; i < items.length; i++){ | |
// items[i].style.backgroundColor = '#f4f4f4'; | |
// } | |
// GETELEMENTSBYTAGNAME // | |
// var li = document.getElementsByTagName('li'); | |
// console.log(li); | |
// console.log(li[1]); | |
// li[1].textContent = 'Hello 2'; | |
// li[1].style.fontWeight = 'bold'; | |
// li[1].style.backgroundColor = 'yellow'; | |
// // Gives error | |
// //items.style.backgroundColor = '#f4f4f4'; | |
// for(var i = 0; i < li.length; i++){ | |
// li[i].style.backgroundColor = '#f4f4f4'; | |
// } | |
// QUERYSELECTOR // | |
// var header = document.querySelector('#main-header'); | |
// header.style.borderBottom = 'solid 4px #ccc'; | |
// var input = document.querySelector('input'); | |
// input.value = 'Hello World' | |
// var submit = document.querySelector('input[type="submit"]'); | |
// submit.value="SEND" | |
// var item = document.querySelector('.list-group-item'); | |
// item.style.color = 'red'; | |
// var lastItem = document.querySelector('.list-group-item:last-child'); | |
// lastItem.style.color = 'blue'; | |
// var secondItem = document.querySelector('.list-group-item:nth-child(2)'); | |
// secondItem.style.color = 'coral'; | |
// QUERYSELECTORALL // | |
// var titles = document.querySelectorAll('.title'); | |
// console.log(titles); | |
// titles[0].textContent = 'Hello'; | |
// var odd = document.querySelectorAll('li:nth-child(odd)'); | |
// var even= document.querySelectorAll('li:nth-child(even)'); | |
// for(var i = 0; i < odd.length; i++){ | |
// odd[i].style.backgroundColor = '#f4f4f4'; | |
// even[i].style.backgroundColor = '#ccc'; | |
// } | |
// TRAVERSING THE DOM // | |
var itemList = document.querySelector('#items'); | |
// parentNode | |
// console.log(itemList.parentNode); | |
// itemList.parentNode.style.backgroundColor = '#f4f4f4'; | |
// console.log(itemList.parentNode.parentNode.parentNode); | |
// parentElement | |
// console.log(itemList.parentElement); | |
// itemList.parentElement.style.backgroundColor = '#f4f4f4'; | |
// console.log(itemList.parentElement.parentElement.parentElement); | |
// childNodes | |
// console.log(itemList.childNodes); | |
// console.log(itemList.children); | |
// console.log(itemList.children[1]); | |
// itemList.children[1].style.backgroundColor = 'yellow'; | |
// // FirstChild | |
// console.log(itemList.firstChild); | |
// // firstElementChild | |
// console.log(itemList.firstElementChild); | |
// itemList.firstElementChild.textContent = 'Hello 1'; | |
// lastChild | |
// console.log(itemList.lastChild); | |
// lastElementChild | |
// console.log(itemList.lastElementChild); | |
// itemList.lastElementChild.textContent = 'Hello 4'; | |
// nextSibling | |
// console.log(itemList.nextSibling); | |
// // nextElementSibling | |
// console.log(itemList.nextElementSibling); | |
// previousSibling | |
// console.log(itemList.previousSibling); | |
// previousElementSibling | |
// console.log(itemList.previousElementSibling);itemList.previousElementSibling.style.color = 'green'; | |
// createElement | |
// // Create a div | |
// var newDiv = document.createElement('div'); | |
// // Add class | |
// newDiv.className= 'hello'; | |
// // Add id | |
// newDiv.id = 'hello1'; | |
// // Add attr | |
// newDiv.setAttribute('title', 'Hello Div'); | |
// // Create text node | |
// var newDivText = document.createTextNode('Hello World'); | |
// // Add text to div | |
// newDiv.appendChild(newDivText); | |
// var container = document.querySelector('header .container'); | |
// var h1 = document.querySelector('header h1'); | |
// console.log(newDiv); | |
// newDiv.style.fontSize = '30px'; | |
// container.insertBefore(newDiv, h1); | |
// EVENTS // | |
// var button = document.getElementById('button').addEventListener('click', buttonClick); | |
// function buttonClick(e){ | |
// //console.log('Button clicked'); | |
// // document.getElementById('header-title').textContent = 'Changed'; | |
// // document.querySelector('#main').style.backgroundColor = '#f4f4f4'; | |
// //console.log(e); | |
// // console.log(e.target); | |
// // console.log(e.target.id); | |
// // console.log(e.target.className); | |
// // console.log(e.target.classList); | |
// // var output = document.getElementById('output'); | |
// // output.innerHTML = '<h3>'+e.target.id+'</h3>'; | |
// // console.log(e.type); | |
// //console.log(e.clientX); | |
// //console.log(e.clientY); | |
// //console.log(e.offsetX); | |
// //console.log(e.offsetY); | |
// // console.log(e.altKey); | |
// // console.log(e.ctrlKey); | |
// // console.log(e.shiftKey); | |
// } | |
var button = document.getElementById('button'); | |
var box = document.getElementById('box'); | |
//button.addEventListener('click', runEvent); | |
//button.addEventListener('dblclick', runEvent); | |
//button.addEventListener('mousedown', runEvent); | |
//button.addEventListener('mouseup', runEvent); | |
//box.addEventListener('mouseenter', runEvent); | |
//box.addEventListener('mouseleave', runEvent); | |
//box.addEventListener('mouseover', runEvent); | |
//box.addEventListener('mouseout', runEvent); | |
// box.addEventListener('mousemove', runEvent); | |
var itemInput = document.querySelector('input[type="text"]'); | |
var form = document.querySelector('form'); | |
var select = document.querySelector('select'); | |
// itemInput.addEventListener('keydown', runEvent); | |
// itemInput.addEventListener('keyup', runEvent); | |
// itemInput.addEventListener('keypress', runEvent); | |
// itemInput.addEventListener('focus', runEvent); | |
// itemInput.addEventListener('blur', runEvent); | |
// itemInput.addEventListener('cut', runEvent); | |
// itemInput.addEventListener('paste', runEvent); | |
// itemInput.addEventListener('input', runEvent); | |
// select.addEventListener('change', runEvent); | |
// select.addEventListener('input', runEvent); | |
form.addEventListener('submit', runEvent); | |
function runEvent(e){ | |
e.preventDefault(); | |
console.log('EVENT TYPE: '+e.type); | |
//console.log(e.target.value); | |
// document.getElementById('output').innerHTML = '<h3>'+e.target.value+'</h3>'; | |
// output.innerHTML = '<h3>MouseX: '+e.offsetX+' </h3><h3>MouseY: '+e.offsetY+'</h3>'; | |
// document.body.style.backgroundColor = "rgb("+e.offsetX+","+e.offsetY+", 40)"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous"> | |
<title>Item Lister</title> | |
</head> | |
<body> | |
<header id="main-header" class="bg-success text-white p-4 mb-3"> | |
<div class="container"> | |
<h1 id="header-title">Item Lister <span style="display:none">123</span></h1> | |
</div> | |
</header> | |
<div class="container"> | |
<div id="main" class="card card-body"> | |
<h2 class="title">Add Items</h2> | |
<form class="form-inline mb-3"> | |
<input type="text" class="form-control mr-2"> | |
<select class="form-control mr-2"> | |
<option value="1">1</option> | |
<option value="2">2</option> | |
<option value="3">3</option> | |
</select> | |
<input type="submit" class="btn btn-dark" value="Submit"> | |
</form> | |
<h2 class="title">Items</h2> | |
<ul id="items" class="list-group"> | |
<li class="list-group-item">Item 1</li> | |
<li class="list-group-item">Item 2</li> | |
<li class="list-group-item">Item 3</li> | |
<li class="list-group-item">Item 4</li> | |
</ul> | |
<div id="output"></div> | |
<br> | |
<button class="btn btn-dark btn-block" id="button">Click Here</button> | |
<br> | |
<div id="box" style="width:400px;height:200px;background:#f4f4f4"><h3>HELLO</h3></div> | |
</div> | |
</div> | |
<script src="dom.js"></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Helpful, Thanks