Quantcast
Channel: Active questions tagged html - Stack Overflow
Viewing all articles
Browse latest Browse all 67497

Toggle button text and paragraph background

$
0
0

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.


Viewing all articles
Browse latest Browse all 67497

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>