fullstack_webdev

Code and notes from Full stack web developer path, LinkedIn Learning.

View on GitHub

DOM

05_01 DOM: The Document Object Model


05_02 Access elements with querySelector methods

document.querySelectorAll("main li:last-child").forEach((item) => {
  item.style.backgroundColor = "blue";
});

document.querySelectorAll("main li").forEach((item) => {
  item.style.backgroundColor = "red";
});
  1. Document.querySelector()
  2. Document.querySelectorAll()

05_03 Access elements using older methods


05_04 Find an Element


05_05 Modifying element classes

  1. Document.getElementsByClassName()
  2. Document.getElementById()

05_06 Attributes

// Checks if the img element has the attribute title and returns true or false
document.querySelector("img").hasAttribute("title");

// getAttribute("attrName") what all that attribute contains
document.querySelector("img").getAttribute("src");

// setAttribute(): Add This shouldn't be here to title attribute in img
document.querySelector("img").setAttribute("title", "This shouldn't be here!");

// removeAttribute(): Remove the attribute by name
document.querySelector("img").removeAttribute("title");

05_07 Inline Style

document.querySelector(".site-title").style;

// Changing individual style properties using .style
document.querySelector(".site-title").style.color = "rebeccapurple";

// put all the style declarations in one go using cssText("prop1: val1; prop2: val2;") - NOT RECOMMENDED
document.querySelector(".site-title").style.cssText =
  "color: rebeccapurple; background-color:pink;";

05_08 Practice: Modify Classes and attributes and styles


05_09 Add DOM Elements


Challenge: 05_10 Add a new Element

  1. Create a element for a nav menu in JS.

  2. Add unordered list with 5 items.

  3. Add new navigation menu to DOM directly after the header.

  4. Write basic CSS for a horizontal menu, use flex or grid to create the horizontal menu layout.


Wrap Up

  1. What method(s) would you use to check if an element has a specific ID and if so replace it with a different ID?

    the element.hasAttribute() and element.setAttribute() methods.

  2. What is the difference in the return from the element.className and element.classList properties?

    element.className returns a string containing all classes appended to the element. element.classList returns a DOMTokenList with each class appended to the element.

  3. What does the HTML markup of this image element look like after the following script has executed?
const newImage = document.createElement("img");
newImage.classList.add("feat-img");
newImage.setAttribute("src", "logo.svg");
newImage.setAttribute("alt", "The company logo");
newImage.style.cssText = "display: block";

<img class="feat-img" src="logo.svg" alt="The company logo" style="display: block;">

  1. What is the value of const target after this code has executed? const target = document.querySelectorAll("a");

    A node list containing each element object matching the query.

  2. What is the value of const target after this code has executed (assuming there are elements in the DOM with the class “note”)? const target = document.getElementsByClassName(".note");

    An empty array-like HTMLCollection object. The getElementsByClassName() method receives a string of classnames to be found as its parameter Adding the . in front of the classname invalidates the query and no match is produced.

  3. The querySelector() and querySelectorAll() methods use what kind of selectors as their parameter?

    A CSS selector string.

  4. What happens if you run this code:
const target = document.querySelector(".first-paragraph");
target.style.font-family = "sans-serif";

“Uncaught SyntaxError: Invalid left-hand side in assignment” JavaScript does not allow for hyphens in property names. When targeting CSS properties, use camelCase, so the “font-family” property becomes “fontFamily”.

  1. What does the element.classList.toggle() method do?

    Adds the specified class if it is not appended to the element, removes the specified class if it is appended to the element.

  2. Where in the HTML document does the new element appear when you use the document.createElement() method?

    Nowhere: The element is created, but has not been added to the DOM.

  3. What is the “DOM”?

    DOM is short for “Document Object Model”, the document object the browser creates when it renders an HTML document.