The teacher ask us to do a JS homework.
JavaScript
window.onload = function() {
var button = document.getElementsByTagName("button");
button[0].onclick = changeBackground;
}
function changeBackground() {
var allParas = document.getElementsByTagName("p");
for (var i = 0; i < allParas.length; i++) {
allParas[i].style.backgroundColor = "yellow";
}
}
Here is the task detail:
Modify the HTML code to add in the button. Write the corresponding JS code (in an unobtrusive manner) to link the button to a function that highlights the paragraphs when clicked. The button should act as a “toggle”, that is, if the paragraphs are already highlighted, then clicking the button unhighlight them. If the paragraphs aren’t highlighted, then clicking the button highlights them. The button’s text should change to reflect this (see below). You can introduce additional variables to make this work.