JavaScript Exercises
Change Background to change page header
1. Write a function to change page background
function changeBG() {
var selectedBGColor = document.getElementById("bgchoice").value;
document.body.style.backgroundColor = selectedBGColor;
var header = document.getElementsByClassName("header")[0];
header.style.backgroundColor = selectedBGColor;
}
select id="bgchoice" onchange="changeBG()">
<option></option>
<option value="red">Red</option>
<option value="ivory">Ivory</option>
<option value="pink">Pink</option>
</select>
Write a function to change the page background:
You will need to put some suitable background images int the images folder
document.body.style.backgroundImage =“url(images/stardust.png)”;
Check Form
Write a function to check that all fields have been filled in:
Replace fname with the names of the form field :
Add an onsubmit event to the form that calls the ValidateForm() function.
function validateForm() {
var x = document.forms[0]["fname"].value;
if (x == "") {
alert("Name must be filled out");
return false;
}
}