rbi-learning / Today-I-Learned

1 stars 0 forks source link

10/28 Week 3, Day 3: APIs, DOM Manipulation, and CSS #187

Open ajlee12 opened 4 years ago

ajlee12 commented 4 years ago

Morning Exercise Review

Let's make a func. that gets the data from the turtles API:

async function getTurtles() {
  const response = await fetch('https://TMNT-API.rbi.repl.co');
  const turtles = await response.json();
  return turtles;
}

Then let's put the turtles onto the DOM after getting data:

async function showTurtles() {
  // Call the func. above. Remember to wait for its results.
  const turtles = await getTurtles;

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

  // Make a div out of each turtle:
  const content = turtles.map((turtle) => {
    return `
      <div>
        <h2>${turtle.name}</h2>
        <p></p>
        <img src="${turtle.image}" alt="${turtle.name}" />
        <h3>Weapon: ${turtle.weapon}</h3>
      </div>
    `;
  }).join('');

  // Insert the divs we made:
  main.innerHTML = content;
}

// Finally, fire everything away:
showTurtles();

Let's style it a bit:

header {
  position: fixed;
  background-color: black;
  color: white;

  /* Note that for the value 0, you don't need to provide a unit. */
  top: 0;
  left: 0;
  right: 0;
}

main {
  /* Flex options */
  display: flex;
  gap: 1rem;
  flex-wrap: wrap;
  margin: auto;
  max-width: 40rem;

  /* Grid options */
  display: grid;
  grid-gap: 1rem;
  grid-template-columns: auto auto;
}

/ Give the

s that contain texts a fixed height / .turtlel p { height: 2rem; font-style: italic; }

/ Turn turtle names into all uppercase / .turtle h2 { text-transform: uppercase; }


## Min-max function for grid-template
- Read about it here: https://developer.mozilla.org/en-US/docs/Web/CSS/minmax

```css
{
  grid-template-columns: repeat(auto-fill, minmax(20rem, 1fr));
}

Dog Age Calculator

Input Field

Let's make an input field in HTML:

<input type="number" id="dog-age" value="1" min="1" required />

It looks like this:

Screen Shot 2020-10-28 at 1 56 49 PM

Make Input Field Interactive

Target the input box with JS:

const dogAgeInput = document.querySelector('#dog-age');

// This is the div where we want to display our calculations later.
const answer = document.querySelector('#answer');

Add an event listener to the input box:

dogAgeInput.addEventListener('input', convert);

The "input" event triggers the convert function, which we define here:

Our form looks like this:

Screen Shot 2020-10-28 at 1 58 13 PM

Style it!

body {
  display: grid;
  place-items: center;
}
form div {
  margin: 1rem auto;
}

Default Behavior of <form>

By default, when you submit a form, the webpage will reload, and the form values are added as query params in the URL.

What if we don't want these default behaviors?

Use JavaScript to preventDefault:

const drinkingAge = 21;
const form = document.querySelector('form');

// Note the "event" param here.
function handleSubmit(event) {
  event.preventDefault();

  // We're accessing the specific input fields within the form by "name".
  const name = form.firstName.value;
  const age = Number(form.age.value);

  if (age >= drinkingAge) {
    // Display something on document.body that cheers the visitor.
  } else {
    // Display some restriction.
  }
}

// Run the func when form is submitted.
form.addEventListener('submit', handleSubmit);