2

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!

1

3 Answers 3

3

I made a fiddle using your js function: http://jsfiddle.net/Ahb8F/

It works fine, did you expect/want something else?

2
  • Thanks 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. Jan 21, 2012 at 9:46
  • Stupid, 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. Jan 21, 2012 at 9:51
0

Your code works fine

http://jsfiddle.net/Z7bMa/

0

That's a bizarre bug. I've just tried doing what you did, and it worked fine for me: http://jsfiddle.net/4aGag/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.