JavaScript JavaScript and the DOM Traversing the DOM Getting the First and Last Child

JORGE HERRANDO GARIJO
JORGE HERRANDO GARIJO
7,895 Points

Keeping the color on the first and last li

Hello. My first and last li have a backgroundColor defined. I am trying to keep the color always in the first and last li even when I change the position. I managed to get the color to the new first-last li, but I cannot make it disappear from the previous one. For any reason, the for loop doesn't go through the list items, yet the console says the backgroundColor it's been changed... Any suggestions?

https://w.trhou.se/alve62v4w1

1 Answer

Steven Parker
Steven Parker
177,725 Points

The "backgroundColor" should be a sub-property of the "style" and not a property of the element itself. And you probably want to just remove the color instead of making it black:

//  lis[i].backgroundColor = 'black';
    lis[i].style.backgroundColor = '';  // fixed
JORGE HERRANDO GARIJO
JORGE HERRANDO GARIJO
7,895 Points

Those little bugs!!

Yeah I was trying black just to let me see if it was working.

Thank youuu!