I am for looping an HTMLCollection with the goal of updating the style of every p
tag. My problem is that I get Invalid left-hand side in assignment expression
and I don’t know why.
var getp = document.getElementsByClassName(currentUser[0]?.username)
for (let i of getp) {
console.log(i?.style);
i?.style.background = 'red'
}
4
Answers
You cannot assign a value to possibly
undefined
property. Instead of using optional chaining, you could use simpleif
condition:It is very simple just remove left-hend side
?
from
to
thanks
you can use the for loop with an Array or just a regular for loop like this
tested and working perfectly
We can simple use
for
loop to resolve this issueIt will work perfectly.