lec-js-04
March 13th: Midterm 2 (JS)
March 15th: Withdraw Deadline
April 5th: Project Submission
April 11th: Final Exam
◉ Great. We can grab DOM elements. Now what?
◉ How do we add/modify an element's text?
◉ How do we add/modify an element's data attribute(s)?
◉ How do we modify an element's appearance?
◉ How do we remove elements?
◉ How do we add elements?
◉ How do we deal with multiple elements?
You can use it for both reading (as we saw Monday) AND writing.
⚠️Be careful: it can be destructive!⚠️
Why? How about this?
So just don't.
And then there's this:
01-text-manipulation
another dot
02-data-attributes
How can we change the colour attribute on the first <p> to "red"? 🤔
How can we add a NEW data attribute called model-number w/ a value TL-241 to the <h1>? 🤔
⚠️ Careful...how do we get the kebob-case?
<h1 style="background: yellow;font-size: 3em;">Inline!</h1>.some-feature {
background: yellow;
font-size: 3em;
}<h1 class="some-feature">Class!</h1>inline style
CSS class
<h1 style="background: yellow;font-size: 3em;">Inline!</h1>let h1Elem = document.querySelector("h1");
h1Elem.style.background = "yellow";
h1Elem.style.fontSize = "3em";⚠️ Each property is set to a string value ("yellow", "3em").
⚠️ CSS property names ("font-size") must be made camelCase ("fontSize").
⚠️
⚠️
⚠️
⚠️
.some-feature {
background: yellow;
font-size: 3em;
}<h1 class="some-feature">Class!</h1>let h1Elem = document.querySelector("h1");
h1Elem.classList.add("some-feature");⚠️ Each class is added via classList.add("class-name").
⚠️ You can add multiple classes at once: add("a", "b", "c") works!
💀Do NOT use className = "blah"! You potentially nuke other classes!
⚠️ You can remove classes with classList.remove("blah").
⚠️ You can toggle classes with classList.toggle("blah").
03a-inline-styles
That "flicker" is a bit disconcerting, ain't it?
What could we do about it?
03b-class-styles
⚠️There's no need to do some kind of loop! ⚠️
04-removing
How can we remove the img element? 🤔
How can we remove all the li elements, leaving the ol intact? 🤔
First way: using createElement.
HINT
It's a great idea to write up the markup you want to create ahead of time. That way, you can test it out to see if it works the way you want - and you then have a blueprint of sorts to work from as you write your JS.
Here's the markup for it:
<div class="game-tile">
<h2 data-bgg-id="35677">Le Havre</h2>
<img src="./img/le-havre-small.jpg" alt="Le Havre" />
</div>Parent div. Note the class.
Need an h2 with text and data attribute.
Need an img with a src and alt.
⚠️ So to make our goal, we need to make 3 separate Elements: a div, an h2, and an img.
<div class="game-tile">
<h2 data-bgg-id="35677">Le Havre</h2>
<img src="./img/le-havre-small.jpg" alt="Le Havre" />
</div>let imgElem = document.createElement("img");
imgElem.setAttribute("src", "./img/le-havre-small.jpg");
imgElem.alt = "Le Havre";Make the img element using document.createElement.
Add the necessary attributes. This is shown here using the setAttribute method and the alt property...but that's just for illustrative purposes - you should pick a lane in your own code!
05a-create-element
Having all that code floating around in a script is pretty confusing - you should totally put it in a well-named function!
Hopefully, you're having one of those "Hey - this is totally useful for the project!" moments right now.
Let's see what this is like.
05b-template
🤔I'm not sure how I feel about this technique yet! How about you?
If you want to do this on the Project, go ahead.
I will not test you on templates (midterm and final exam).
looping
using map()