Self-Paced Work: Background Images
Follow the instructions below to create a webpage that allows the user to change the background image dynamically. By the end, the user should be able to click buttons to change the background, and enter their own background image URLs too! Use previous HTML and JavaScript files as references.
Reference: JavaScript Code to Update the Background Image
Later on in this exercise, it will be necessary to update the background image of the page using JavaScript. Here is how to do that. Replace <your image file here>
with the filename of the image:
document.body.style.background = "url(<your image file here>)";
Refer back to this code when it is needed.
Part 1 - Setup
Start by remixing this starter project, then update the initial background image. In the style.css file, use the following code:
body {
background: url(https://github.com/hytechclub/web-102/blob/master/Assets/City.jpg?raw=true);
}
This is the ruleset that will be updated with JavaScript when buttons are clicked.
Part 2 - One Button
Add a button that will set the background image when clicked.
JavaScript
Start by defining a function in JavaScript.
- Open the script.js file for editing
- At the top of the file, define a new function named
changeBackground
function
keyword- Function name:
changeBackground
- Parentheses (left/right)
- Curly brackets (left/right)
- In the body of the function (between
{
and}
), make a new line - On the new line, add the code to update the background image to https://github.com/hytechclub/web-102/blob/master/Assets/Pokemon.jpg?raw=true
Now the function should be defined... but it needs to be called!
HTML
Add a button in the HTML to call the newly-defined function.
- Open the index.html file for editing
- Within the
<body></body>
, create a<button>
element - Make the text for the button be
Image 1
- Set the
onclick
of the<button>
to call thechangeBackground
function- Use the function name and parentheses (left/right)
At this point, load up the page and try clicking the button. The background image should update!
Part 3 - Two Buttons
Now, create another HTML <button>
and JavaScript function that will change the background to another image. This will be very similar to the steps above!
JavaScript
In the script.js function, define a new function named changeBackground2
. It should be almost exactly the same as the changeBackground
function, except that it should set the background URL to https://github.com/hytechclub/web-102/blob/master/Assets/City.jpg?raw=true.
HTML
In the index.html file, create a new <button>
. Place it under the existing <button>
, within the <body></body>
. It should be almost exactly the same as the other <button>
, but the text should be Image 2
and the onclick
attribute should call the changeBackground2
function.
Once the functional button has been added, load up the page and click it. It should change the background to the new image!
Part 4 - Parameter Time
At this point, the script.js file should contain two very similar functions. The only differences are the names of the functions, and the actual filenames for the images. Looks like a parameter could help simplify this code!
Updating the Function Definition
Start by changing one of the existing function definitions so that it uses a parameter.
- Open the script.js file for editing
- In the
changeBackground
function definition, add a parameter namedbackgroundUrl
- It should go in between the parentheses right after the function name
- In the body of the
changeBackground
function, replace the image URL with the variable value of the parameter- Using a template literal, it should look something like this:
`url(${backgroundUrl})`;`
- Using a template literal, it should look something like this:
Now the function has been redefined, which means it can set the background to any image!
Updating the Function Calls
Redefining the function means that the function call is currently broken. Update each function call so they each pass in an argument.
- Open the index.html file for editing
- Find the
<button>
where theonclick
attribute calls thechangeBackground
function - Update this call by placing
'https://github.com/hytechclub/web-102/blob/master/Assets/Pokemon.jpg?raw=true'
between the parentheses - this will be the argument- Note that the argument value should be surrounded by single quotes; this is because the attribute is within double quotes
- Update the second
<button>
to callchangeBackground
and pass in'https://github.com/hytechclub/web-102/blob/master/Assets/City.jpg?raw=true'
as the argument
Now try loading up the page. Click each button and verify that they still work as intended!
At this point, it will be possible to remove the changeBackground2
function; it should not be needed any longer.
Part 5 - Three Buttons
In addition to refactoring the other buttons, adding this parameter makes it much simpler to add another new button! Note that no changes in the script.js file are required.
- Open the index.html file for editing
- Within the
<body></body>
, under the existing<button>
s, create a new<button>
element - Make the text for the button be
Image 3
- Set the
onclick
of the<button>
to call thechangeBackground
function- Use the function name and parentheses (left/right)
- Pass in an argument of
'https://github.com/hytechclub/web-102/blob/master/Assets/CalvinAndHobbes.jpg?raw=true'
to the function
Load up the page again, click the third button, and verify that the background changes to yet another new image! All this without changing anything in the script.js file 😎
Challenge 1 - Prompt for Image
Try to expand the functionality of the page by adding a button that, when clicked, will prompt the user for a URL, and set the background image to whatever they enter.
JavaScript
Start by defining a new function in the JavaScript.
- Open the script.js file for editing
- Under the existing function, define a new function named
promptForBg
- In the body of the
promptForBg
function, use aprompt
to ask the user what they want the background to be- Make sure to store their answer in a variable
- Under that prompt, call the
changeBackground
function and pass in the value the user entered as the argument
That should do it for the JavaScript!
HTML
To actually use the promptForBg
function, it will be necessary to add a button to the page.
- Open the index.html file for editing
- In the
<body></body>
, under the existing<button>
s, add a new<button>
- Make the text for the button be
Image from Prompt
- Set the
onclick
attribute for the<button>
to call thepromptForBg
function- Note that it is not necessary to pass in a URL here; the function will take care of that with the
prompt
!
- Note that it is not necessary to pass in a URL here; the function will take care of that with the
Load up the page, click the button, and verify that a new URL can be entered! Try to find an image somewhere online and copy its URL for fun custom backgrounds!
This is a good example of reusing code to implement new features. By calling the changeBackground
function, it was possible to avoid unnecessary code duplication!
Challenge 2 - Input for Image
Finally, expand the functionality a little more by creating a button that will set the background image based on an input on the page.
HTML
Start by updating the HTML with the necessary elements.
- Open the index.html file for editing
- In the
<body></body>
, under the<button>
elements, create a new<div>
element - Within the
<div></div>
, create a new<input />
element - Set the
type
attribute of the<input />
element to be"text"
- Set the
id
attribute of the<input />
element to bebg-input
- Under the
<input />
element, still within the<div></div>
, create a<button>
- Make the text for the
<button>
beImage from Input
Now the elements should all be in place - but the button needs something to do!
JavaScript
Define a new function to set the background image based on the new text box element.
- Open the script.js file for editing
- Under the existing function, define a new function named
inputForBg
- In the body of the
inputForBg
function, create a new variable namedbgInputElement
- Set the variable to be a call to the
document.querySelector
function- It should select the
<input />
from the HTML based on itsid
- It should select the
- Under that, create another new variable named
bgInputValue
- Set that variable to be the
.value
of thebgInputElement
variable - Under that, call the
changeBackground
function, and pass in thebgInputValue
variable as the argument - Under that, set the
.value
of thebgInputElement
variable to be empty (""
)
That should be it for the JavaScript! The function is defined and ready to go... it just needs to be called from the HTML.
Back to the HTML
Update the new button so that it does the right thing.
- Open the index.html file for editing
- Find the
<button>
element with the textImage from Input
- Set the
onclick
attribute for that<button>
to call theinputForBg
function
That's it! Load up the page, enter a URL into the text box, and click the button. The new background image should appear, and the text box should go back to being empty!