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;
}
/* Center the text within the `turtle` divs. */
.turtle {
text-align: center;
}
/ 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));
}
auto-fill: Let the browser decide however many columns there should be
Let each column have min 20rem width
When the window view gets too small for more than 1 column,
Note that the value cannot go below 1 and the default is 1.
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');
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);
Morning Exercise Review
Let's make a func. that gets the data from the turtles API:
Then let's put the turtles onto the DOM after getting data:
Let's style it a bit:
And here for CSS Flexbox: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
object-fit
property: https://www.w3schools.com/css/css3_object-fit.asp/ 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; }
auto-fill
: Let the browser decide however many columns there should befr
: fractional unitfr
in CSS Grid here: https://www.digitalocean.com/community/tutorials/css-css-grid-layout-fr-unitDog Age Calculator
Input Field
Let's make an input field in HTML:
<input>
is a self-closing tag.It looks like this:
Make Input Field Interactive
Target the input box with JS:
Add an event listener to the input box:
The "input" event triggers the
convert
function, which we define here:.value
property.Note that we're turning the stringified input values into numbers.
Afternoon Review
<form>
An example:
id
and camelCase forname
.name
attribute, and JS doesn't recognize kebab case.for
attributes for your labels.for
to those ofid
.We wrapped each
<input>
and<button>
in a<div>
so they stack up vertically.Our form looks like this:
Style it!
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
:preventDefault()
is an Event Method; read about it here: https://www.w3schools.com/jsref/event_preventdefault.asp