i want to change color all 'p' elements, i tried do it with this code but it not works.
document.getElementsByTagName('p').forEach(element => {
element.style.color = 'white'
});
Is there a right way(without using JQuery)?
i want to change color all 'p' elements, i tried do it with this code but it not works.
document.getElementsByTagName('p').forEach(element => {
element.style.color = 'white'
});
Is there a right way(without using JQuery)?
Share Improve this question asked Jun 23, 2018 at 21:09 H.DenisonH.Denison 531 silver badge5 bronze badges 4-
document.querySelectorAll('p').style.color = 'white'
try that way. – Vladimir Jovanović Commented Jun 23, 2018 at 21:14 -
^ this returns a
NodeList
, not anHTMLCollection
, which can be iterated over. You can't just set thestyle.color
for theNodeList
, though! – chemicalcrux Commented Jun 23, 2018 at 21:16 -
1
Yeah, you're right @chemicalcrux. My bad. But this code works
document.querySelectorAll('p').forEach(e => e.style.color = "white")
– Vladimir Jovanović Commented Jun 23, 2018 at 21:17 - First, that is not jQuery, second, how is that code piece not working? ... Provide a code snippet reproducing the issue described. – Asons Commented Jun 23, 2018 at 21:47
5 Answers
Reset to default 3You can acplish this using the vanilla JavaScript ES2015 (ES6) code.
document.querySelectorAll('p').forEach(e => e.style.color = "white");
UPDATE:
I forgot to mention that this is applying white color of the element in the DOM which is not so efficient, so you can also do this in another way.
CSS:
.white {
color: #fff;
}
JavaScript:
// If you have just one class for the element
document.querySelectorAll('p').forEach(e => e.className = 'white');
// If you are appending the class of `white`
document.querySelectorAll('p').forEach(e => e.className += ' white');
You can do the same like this
var list= document.getElementsByTagName('p');
for (var i = 0; i < list.length; i++) {
list[i].style.color = "white";
}
As document.getElementsByTagName
returns HTMLCollection type object you have to iterate using above method as per this answer
You can't iterate over the HTMLCollection
returned by getElementsByTagName
. Converting it to an Array
will work, though. Use Array.from()
to do this:
Array.from(document.getElementsByTagName('p')).forEach(element => {
element.style.color = 'white'
});
All you have to do here is to make a simple loop.
let el = document.getElementsByTagName('p')
let i
for (i = 0; i < el.length; i++) {
el[i].style.color = 'white'
}
document.getElementsByTagName('p')
Return HTMLCollection(6) [p, p, p, p, p, p.left]
This is a HTML Collection.
So if you check
document.getElementsByTagName('p') instanceof Array
It returns false
.
As HTML Collections are not array's so you cannot perform .forEach()
.
So you must convert it into an array by using Array.from()
.
The
Array.from()
method creates a new, shallow-copied Array instance from an array-like or iterable object.
Array.from(document.getElementsByTagName('p')).forEach(element => {
element.style.color = 'white'
});
So this is the solution for your problem.