-->
Showing posts with label id in JS. Show all posts
Showing posts with label id in JS. Show all posts

Friday, June 20, 2025

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>