-->

Friday, June 20, 2025

JavaScript Program to Use getElementsByTagName() to change the color of paragraphs | DOM Manipulation in JS with Examples


JS DOM programs collection


JavaScript Program  to change the color of different paragraphs  using getElementsByTagName(DOM)


<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<title>color of different paragraphs</title>

</head>

<body>

<p>text1</p>

<p>text2</p>

<p>text3</p>

<p>text4</p>

<button onclick="highlights()">click to highlight the paragraphs</button>

<script>

function highlights()

{

let i;

item=document.getElementsByTagName('p');

for(i=0;i<=3;i++)

{

item[i].style.backgroundColor="orange";

}

}

</script>


</body>

</html>

JavaScript Program to Use getElementById() to change the color of paragraphs with different ids | DOM Manipulation in JS with Examples


JS DOM programs collection




JavaScript Program to Use getElementById() to change the color of different paragraphs having different ids  using getElementById (DOM)



<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>looping through multiple ids</title>
</head>
<body>
<p id="i1">text1</p>
<p id="i2">text2</p>
<p id="i3">text3</p>
<p id="i4">text4</p>
<button onclick="highlights()">click to highlight the paragraphs</button>
<script>
function highlights()
{
let i;
for(i=1;i<=4;i++)
{
item=document.getElementById('i'+i);
item.style.backgroundColor="orange";
}
}
</script>
</body>
</html>