Andrew Clear
Andrew Clear

Reputation: 8020

Javascript to change color of specific <li> of an <ul>

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!

Upvotes: 2

Views: 12745

Answers (3)

visualidiot
visualidiot

Reputation: 550

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

Upvotes: 0

dpington
dpington

Reputation: 1884

Your code works fine

http://jsfiddle.net/Z7bMa/

Upvotes: 0

Rick Kuipers
Rick Kuipers

Reputation: 6617

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

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

Upvotes: 3

Related Questions