rbi-learning / Today-I-Learned

1 stars 0 forks source link

10/27 Week 3, Day 2: CSS, DOM Manipulation #186

Open ajlee12 opened 3 years ago

ajlee12 commented 3 years ago

StarWars Species Exercise

CSS

SpeciFISHity Cheatsheet!

Center the table

Border collapse

Check out what border-collapse does here. Screen Shot 2020-10-27 at 7 46 02 AM

Text transform

You can capitalize all letters by setting the text-transform property:

{ text-transform: capitalize; }

Apply hovering rules:

body {
  background-color: black;
}

tr:hover {
  background-color: darkgrey;
}

While Loop

let myNum = 8;

while (myNum < 29) {
  console.log(myNum);
  myNum++;
}

This loop will print out myNum's values from 8 to 28 in the console.

The loop runs while myNum is less than 29.

Try-Catch

We "try" something, and if it produces an error, we "catch" it.

try {
  const response = await fetch(`api_url`);
  const data = response.json();
} catch(error) {
  throw error;
}

Event Listeners

Quick overview of event listeners: https://www.w3schools.com/js/js_htmldom_eventlistener.asp

Let's target a button first:

const button = document.querySelector('button');

Now let the button react to clicks:

button.addEventListener('click', _someFunction_);

Upon clicking this button, the function _someFunction_ will be called.

Class vs. ID

Let's say you have some buttons on the HTML:

<div class="button-wrapper">
  <button id="A">I'm Button A</button>
  <button id="B">I'm Button B</button>
</div>
<div class="button-wrapper">
  <button>Random Button</button>
  <button>Random Button</button>
</div>

Using JS to target specific elements:

// Selecting Button A
const btnA = document.querySelector('#A');
// Selecting Button B
const btnA = document.querySelector('#B');

// Selecting the 2 divs with class name "button-wrapper"
const divs = document.querySelectorAll('.button-wrapper');

You can target them in CSS to apply targeted styling:

div.button-wrapper {
  /* You can style both divs here */
}
button {
  /* This styles ALL 4 buttons */
}
button#A {
  /* This only styles Button A */
}

Event Target

We have the button within a <div>:

<div class="meal">
  <div class="dish"></div>
  <button id="chicken">Chicken Button</button>
</div>
The meal is...

Now, define a function that's triggered by a button-click event: ```JS // Note the "event" param. function handleClick(event) { // Target the button that got clicked. const theBtnThatGotClicked = event.currentTarget; // Find the cloest div with class name "meal". const theClosestMeal = theBtnThatGotClicked.closest('.meal'); // Select the div with class "dish" within the "meal" div. const theDishInside = theClosestMeal.querySelector('.dish'); } ``` Then let's write another function that ```JS function cook(rawEmoji) { const food = { '🐓': '🍗', '🐄': '🍔', '🌽': '🍿', }; return food[rawEmoji]; } ``` Now we can add the functionality of "cooking" to finish off this previous func.: ```JS // Note the "event" param. function handleClick(event) { const theBtnThatGotClicked = event.currentTarget; const theClosestMeal = theBtnThatGotClicked.closest('.meal'); const theDishInside = theClosestMeal.querySelector('.dish'); // Cook using the `cook` function! theDishInside.textContent = cook(theDishInside.textContent); theBtnThatGotClicked.disabled = true; theBtnThatGotClicked.textContent = 'Cooked!'; } ```