Session 18: Responding to Button Clicks with JavaScript
Download this starter code to use for the opening exercise and the rest of the session.
Opening Exercise: Change the Text and Style of a Page
The reading for this week shows us how we can access and change an element's content and styling, perhaps in response to a user action. Consider these applying these JavaScript statements to our demo page from last time:
myMain = document.body.querySelector('p'); myMain.innerHTML = '<main>Smile 2 tops the box office.</main>'; myMain.style.height = '200px'; myMain.style.backgroundColor = 'lightgreen'; myMain.style.width = '200px'; document.body.style.backgroundColor = 'slategray';
Let's practice.
In particular:
-
The
h1
heading is smaller than theh2
heading, because the style sheet sets the size ofh1
but leavesh2
at the default size.
Change the size ofh2
, or the size ofh1
. -
The text in the
.infobox
element crowds right up to the edge of the border box.
Add padding to the element. The bottom padding should be a bit larger than the top.
Put your code in the file script.js, which is loaded by the web page.
Solution
Here is a possible solution.
Recall that JavaScript uses different naming conventions than CSS, so we have to use camelCase instead of hyphens. JavaScript also use string values for all name/value pairs.
Reviewing Functions
Last time, we learned about JavaScript functions. A function
- is a named piece of code
- that groups statements together
- and makes code reusable.
We define functions and call them.
function soundAlarm() { alert("Wake up before you go-go!"); } soundAlarm();
Recall the evolution of our code on the Road to St. Ives in Session 17:
- First we wrote statements to calculate the number of travelers.
-
Then
we moved those statements into a function. We could have put
the code for updating the web page into the function, too.
Had we done so, we might have named the function
countTravelersAndUpdatePage()
. That would have made the function less useful to us, though, because it would have tied the calculation to the way we used its value. So we left the code for updating the web page outside, where we use the function. -
Finally
we added an argument to the function. This enables us to
call the function with different values (9? 42?) in different
places, and use the results however we see fit — say,
to add a new
p
element for the 9-wives version.
You will get more practice writing functions in Homework 8. If functions are new to you, then you may be fuzzy on how calling a function and passing arguments to it works. There will be a section in the read for Week 10 that walks through the process.
Working with Elements in the DOM
Each element in the DOM contains a large number of properties and methods. Once we have a reference to an element, we can use these properties and methods to manipulate it.
Element Properties
We have already worked with two properties of HTML elements.
element.innerText
holds the text content of the
element. element.innerHTML
holds the markup
content of the element, which could be text, but could also
include other HTML tags.
Other useful properties include:
-
element.id
holds the element'sid
, as in<p id="intro"></p>
. -
element.className
holds the value of the element'sclass
attribute. -
element.classList
holds a collection of all the classes applied to the element. element.parentNode
is a reference to the element's parent, that is, the node that contains it in the DOM.
Element Methods
In addition to querySelector()
and
querySelectorAll()
, there are a several other
methods that are useful for accessing and working with an
element's attributes:
-
element.hasAttribute(name)
checks to see if the attributename
exists on thiselement
. -
element.getAttribute(name)
retrieves the value of the given attribute. -
element.setAttribute(name, value)
sets the value of attributename
to bevalue
. -
element.removeAttribute(name)
deletes the given attributename
from the element.
Let's use setAttribute()
and
removeAttribute()
to implement a common dynamic
behavior on the web: revealing hidden content.