user6363467
user6363467

Reputation: 124

How to change HTML style by taking the class name using javascript (no jQuery allowed)?

I tried this so far and if I take the element id is working but if I take the class name is not working...

<!DOCTYPE html>
<html>
<body>

<p id="p1" class="theClass">Hello World!</p>
<p id="p2">Hello World!</p>

<script>

document.getElementById("p2").style.color = "blue";
document.getElementsByClassName("theClass").style.color = "blue";

document.getElementById("p2").style.fontSize = "larger";
</script>

<p>The paragraph above was changed by a script.</p>

</body>
</html>

This is the output:

enter image description here

Upvotes: 3

Views: 3460

Answers (3)

Muhammad Russell
Muhammad Russell

Reputation: 393

Try this:

document.getElementsByClassName("theClass")[0].style.color = "blue";

Upvotes: 1

Muhammad Usman
Muhammad Usman

Reputation: 10148

document.getElementsByClassName give you the list of the nodes. And there is no style property on the list. You'll have to iterate through the list to apply styles.

If there is just on div there you can use

document.getElementsByClassName("theClass")[0].style.color = "red" 

PS. You should always check your browser's console when something is not working. You'll be seeing an error like can not ... .style of ...

document.getElementById("p2").style.color = "blue";
document.getElementsByClassName("theClass")[0].style.color = "blue";

document.getElementById("p2").style.fontSize = "larger";

var els = document.getElementsByClassName('theClass1');

for(var i=0; i< els.length; i++){
   els[i].style.color = "green"

}
<!DOCTYPE html>
<html>
<body>

<p id="p1" class="theClass">Hello World!</p>
<p id="p2">Hello World!</p>

<p id="p12" class="theClass1">Hello World 1!</p>
<p id="p14" class="theClass1">Hello World 2!</p>
<p id="p13" class="theClass1">Hello World 3!</p>

<p>The paragraph above was changed by a script.</p>

</body>
</html>

Upvotes: 2

CertainPerformance
CertainPerformance

Reputation: 370679

getElementsByClassName returns an HTMLCollection, not an element. If you want to apply a style to an element in the collection, select that element first:

document.getElementsByClassName("theClass")[0].style.color = "blue";
<p id="p1" class="theClass">Hello World!</p>

But if you're going to select a single element, it would be much better to use querySelector:

document.querySelector('.theClass').style.color = "blue";
<p id="p1" class="theClass">Hello World!</p>

Even if you did need to apply a style (or do something) to multiple elements with a class name in common, the getElementsBy* methods return HTMLCollections, which can be difficult to work with. Consider using querySelectorAll instead, which returns a static NodeList - unlike an HTMLCollection, it can be iterated over directly, it won't change while it's being iterated over, and it's much more flexible.

document.querySelectorAll('.theClass')
  .forEach(p => p.style.color = "blue");
<p class="theClass">Hello World!</p>
<p class="theClass">Hello World!</p>
<p class="theClass">Hello World!</p>

Upvotes: 3

Related Questions