Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of .style is not canging element without wasting too much if your time.
The question is published on by Tutorial Guruji team.
The question is published on by Tutorial Guruji team.
I have this script:
price2 = document.getElementById('price2'); price3 = document.getElementById('price3'); for (var i = 0; i < price2.length; i++) { price2[i].style.backgroundColor = 'orange'; } <div class="single-car-prices"> <div class="single-regular-price text-center" id="price2"> <span class="labeled">Ціна</span> <span class="h3"> $ 1.500</span> </div> </div>
Why color of this div is still blue:
There is no errors in console, I think I wrote JS correctly
Answer
getElementById takes always just one element. What you want is:
let price2 = document.getElementById('price2'); let price3 = document.getElementById('price3'); price2.style.color = 'orange';
UPDATE:
If you want to update background
, then just use background
property:
price2.style.background = 'orange';
An example:
let price2 = document.getElementById('price2'); let price3 = document.getElementById('price3'); price2.style.background = 'orange';
<div class="single-car-prices"> <div class="single-regular-price text-center" id="price2"> <span class="labeled">Ціна</span> <span class="h3"> $ 1.500</span> </div> </div>
We are here to answer your question about .style is not canging element - If you find the proper solution, please don't forgot to share this with your team members.