I have multiple h2
enclosed lines of text. The h2
color is specified in the style
section. I am changing backgrounds and, because of contrast issues, also need to change the text when the transition background script int++
in the while
loop hits a certain value of if int===
. The 'h2' lines are contained in some HTML5 nested 'divs'. I have tried attaching color to the 'div' 'id's but failed there and have since moved on successfully, but with potentially massive overhead as a generic solution, to setting a class="forColorChange"
in each h2
tag. However, with multiple lines of text it gets tedious to write out and access each one individually. Is there a code line of JavaScript that basically says in a batch efficiency, turn every 'h2' or, everything class tagged with forColorChange
, from (the style section) color: Green
to in one swoop, 'color:Orange'? My copy and paste runnable HTML5 skeletal stub is in the code section below.
<!doctype html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="description" content=""/>
<meta name="keywords" content=""/>
<meta name="author" content=""/>
<title>Change Font Color of Multiple <code><H2></code> Lines</title>
<!-- <link rel="stylesheet" type="text/css" href="./CSS-MasterStyle2019.css"/> -->
<style>
div#masterPoetryBlockDiv { }
main#mainStyle { }
article#articleStyle { }
.general-formatting-1 { text-indent:5em; }
.general-formatting-2 { text-indent:10em; }
.forColorChange { }
h1 { text-shadow: 1px 0px 1px Purple, 2px -1px 2px Gray;
color:LightBlue;
}
h2 {/* text-shadow: 1px 0px 1px Purple, 2px -1px 2px Gray; */
color:Yellow;
}
body, #b0 {
background-color: RGB(190, 181, 168) ;
background-image: url('./Your-0.jpg') ;
position: relative ;
z-index: -2 ;
background-position: center top ;
background-repeat: no-repeat ;
background-attachment: fixed ;
background-size: cover ;
transition: background 0ms ease-in-out 400ms;
}
#b1 {
background-color: DarkGray ;
background-image: url('./Your-1.jpg') ;
position: relative ;
z-index: -2 ;
background-position: center top ;
background-repeat: no-repeat ;
background-attachment: fixed ;
background-size: cover ;
transition: background 500ms ease-in-out 400ms;
}
#b2 {
background-color: rgb(46, 46, 46) ;
background-image: url('./Your-2.jpg') ;
position: relative ;
z-index: -2 ;
background-position: center top ;
background-repeat: no-repeat ;
background-attachment: fixed ;
background-size: cover ;
transition: background 500ms ease-in-out 0ms;
}
</style>
<script>
var int = 0;
function shiftbackgrd(interval, frames)
{"use strict";
var swap = window.setInterval(IncrementCuckooBkgrnds, interval);
function IncrementCuckooBkgrnds()
{
if(int === 2 ) {clearInterval(swap);}
while (int !== 2 )
{
int++;
document.body.id = "b"+int;
/* Asking the question for an easier BATCH type color change of all Tag '<h2>' (or by class 'forColorChange') and do away with the heavy overhead within the 'ifs' below where every individual line, albeit successful, is being accessed. It is definitely not good for a large number of lines or a high int count.*/
var colorCall = ["Red", "SkyBlue"];
const changeColor = document.getElementsByClassName("forColorChange");
if(int === 1) {
changeColor[0].style.color = colorCall[0];
changeColor[1].style.color = colorCall[0];
changeColor[2].style.color = colorCall[0];
changeColor[3].style.color = colorCall[0];
changeColor[4].style.color = colorCall[0];
changeColor[5].style.color = colorCall[0];
changeColor[6].style.color = colorCall[0];
}
if(int === 2) {
changeColor[0].style.color = colorCall[1];
changeColor[1].style.color = colorCall[1];
changeColor[2].style.color = colorCall[1];
changeColor[3].style.color = colorCall[1];
changeColor[4].style.color = colorCall[1];
changeColor[5].style.color = colorCall[1];
changeColor[6].style.color = colorCall[1];
}
if(int !== 2 ) {break;}
}
}
}
shiftbackgrd(2400, 3); //milliseconds, frames
</script>
</head>
<body>
<div id="masterPoetryBlockDiv">
<main id="mainStyle">
<article id="articleStyle">
<br/><h1 class="general-formatting-1">Cuckoo Clock Schematics</h1><br/>
<h2 class="forColorChange general-formatting-1">Most times</h2>
<h2 class="forColorChange general-formatting-2">one's cuckoo clock</h2>
<h2 class="forColorChange general-formatting-2">interpretation of reality,</h2>
<h2 class="forColorChange general-formatting-1">Is bigger</h2>
<h2 class="forColorChange general-formatting-2">than one's ability</h2>
<h2 class="forColorChange general-formatting-2">to comprehend</h2>
<h2 class="forColorChange general-formatting-2">its workings.</h2>
</article>
</main>
</div>
</body>
</html>