id: an element ID in the DOM- Retrieves the element from the DOM
- Retrieve every element with class of
rice - Returns a HTML collection (not an array!)
- Retrieve every element with class of
nice - Returns a HTML collection (not an array!)
- We can use
forloops with HTML collections, but what if we want to do cool array stuff?
- Converts an HTML collection into an array
ex.
Array.from(collection).forEach(item => {
console.log(item);
});- Selects the element with id
id - Can get quite specific (using CSS selectors)
document.querySelector('#book-list li:nth-child(2) .name');- Grabs element of id
booklistwhich is the 2ndlielement of classname - Only returns first element
- Grabs every element according to the selector
- Returns a Node List (not an HTML collection, not an array!)
- We do not have to turn it into an array
- The text content inside the HTML element
elem
- Change the HTML
ex.
elem.innerHTML += '<h1>epic epic epic!!!</h1>'- Everything in the DOM is a node
- A number representing the node type
- The node name
- Returns true/false
- Clones the element
- Argument
trueclones all the child node rather than just the top one
- Returns an HTML collection of forms
document.forms[0]
document.forms['id']- When clicking a button in a form, the form emits a
submitevent that also refreshes the page
const value = form.querySelector('input[type="text"]').value;- Grab input from form
- Grabs reference to the parent node
- Basically the same as below
- Grabs reference to the parent element
- Basically the same as above
You can chain these together to traverse upwards .parentElement.parentElement.parentElement
- A node list of child nodes
- Includes
textelements which are line breaks
- Only element children, no filler
textnodes
- The next/previous sibling node (includes
textline break)
- The next/previous element (no
textline breaks)
ex.
elem.previousElementSibling.querySelector('p').innerHTML += '<h1>epic!!!!!</h1>'- Select
<p>only from previous element and addepic!!!!!as an<h1>to the HTML
- Attach event listeners on elements to listen for events
elem.addEventListener('click', e => {
console.log(e.target); // target of event
console.log(e); // event itself
})e.targetis the element
- ex. prevent default behaviour of
<a>link
elem.addEventListener('click', e => e.preventDefault());- Default: The event bubbles up from the target element
- The event bubbles up to the parent element, and if there is an event listener on the parent element the callback function would also fire as well
- Bubbling keeps happening until we hit the top
ex. Attaching event listeners to every button is expensive, we can use bubbling to our advantage
list.addEventListener('click', e => {
if(e.target.className == 'delete'){
//grab the ul
const li = e.target.parentElement;
list.removeChild(li);
}
});- ex. list is a
ulelement and we want to remove one of itslielements that containbtnwith classdeleteas children
- Creates a div element
- Floating around, not attached to anything
- Appends
nodeto the DOM
- Set element CSS
- Element class
- A list of the element's classes
- Add/remove a class to
elem - Preferred over
elem.className += '...'
- Gets element attribute
elem.getAttribute('class');
elem.getAttribute('src');- Sets element attribute
- First parameter is attribute, second parameter is what to set to
elem.getAttribute('class', 'cool');
elem.getAttribute('src', 'photo.png');- Returns true or false
- Removes element attribute
document.addEventListener('DOMContentLoaded', () => {});- Attaches everything in {} once the DOM is ready