Javascript to change color of specific <li> of an <ul>
10,304
I made a fiddle using your js function: http://jsfiddle.net/Ahb8F/
It works fine, did you expect/want something else?
Author by
Andrew Clear
Updated on June 04, 2022Comments
-
Andrew Clear almost 2 years
I'm trying to use javascript to change the color of a specific li of a ul, but my code is highlighting all subsequent li of the list. Here is the code:
<ul list style type = "none"> <li id = "l0">this is line one</li> <li id = "l1">this is line two</li> <li id = "l2">this is line three</li> </ul>
function highlight(name,color) { var a = document.getElementById(name); a.style.color = color; }
When I call this with something like
highlight("l0","orangered");
it changes the colors of "l0" through "l2", instead of just "l0". I would very much prefer a solution using only javascript, rather than a 3rd party library. Thanks!
-
Andrew Clear over 12 yearsThanks for the quick reply. Odd that the version I made up on the fly for the post works, and my page doesn't. When I find the discrepancy I will post where I went wrong.
-
Andrew Clear over 12 yearsStupid, stupid, stupid. My problem in the original code was a missing /li tag, so when that line was highlighted (which just happened to be the first line I was testing) it highlighted every subsequent line. In other words, memo to me: before posting look closer.