2023-05-16 DOM Manipulation
main topics
- webpack: bundler for frontend assets
- DOM: Document Object Model
- document.querySelector()
- document.querySelectorAll()
- element.style.{CSS_PROPERTY}
- element.classList.[add|remove|toggle]
- element.setAttribute(attributeName, value)
- element.removeAttribute(attributeName)
 
- to memorize:
- get the element (querySelector)
- define a function
- element.addEventListener(event, function)
 
- get the element (
main takeaways
What's the DOM
Trying to explain with my words:
- DOM is a living HTML
- The HTML file is how the DOM is started. After started, the DOM has its own life.
DOM Manipulation
In my understanding DOM manipulation usually boils down to these 3 steps:
- select an element
- define a function you want to execute
- define which event, happening in the element, should trigger the function.
Such steps are usually performed using these techniques:
- document.querySelector()or- .getElementById()
- write a function receiving an eventas argument
- element.addEventListener(event, function)
Note
- document.querySelector(cssSelector)returns the first element matching the- cssSelector.
- document.querySelectorAll(cssSelector)returns an array with all elements matching the- cssSelector
Changing the Style
const element = document.querySelector(CSS_SELECTOR);
// hiding the element
element.style.display = "none"; // Hide
element.style.display = ""; // Show
// change the background
element.style.backgroundColor = "#fce";
Important
The CSS properties are written in kebab-case (words separated by a dash), but in JavaScript they are in camelCase.
Example: background-color = el.style.backgroundColor
Adding / Removing classes
element.classList.add("red");
element.classList.remove("red");
element.classList.toggle("red");
Editing the contents of an element
Edit the text inside an element:
// the contents are always literal
element.innerText = "new text"
// this string is going to be considered as text
element.innerText = "this will <em>NOT</em> be interpreted as an HTML tag"
Edit the HTML inside an element
// the HTML tags here will emphasize the text
element.innerHTML = "I <em>love</em> Ruby"
// creating a link
element.innerHTML = `<a href="${url}">${linkDescription}</a>`
getting value from input fields
<!-- Some HTML -->
<input name="email" id="email" value="paul@gmail.com" />
const emailInput = document.getElementById("email");
console.log(emailInput.value);       // Read
emailInput.value = "john@gmail.com"; // Write
some events
Full list of events: https://developer.mozilla.org/en-US/docs/Web/Events
# most used during the bootcamp
click   # any visible element
submit  # form
keyup   # window / any focused element
# also common
DOMContentLoaded # document
blur             # input / textarea
change           # select
focus            # any visible element
mouseover        # any visible element
resize           # window
scroll           # window / any scrollable element
touchstart       # for mobile devices
avoiding the default behavior
element.preventDefault()
An example of how it's useful is to prevent the reload of the page when a form is submitted.
const input = document.getElementById('input-text');
input.addEvenetListener('submit', (event) => {
  input.preventDefault();
  // do something cool...
});