Changing color JavaScript doesn't work -
my problem is: have 3 elements on list, , have keep changing text color when mouse hover.
so building 3 different functions, because colors different.
what did is:
<script type="text/javascript"> var links = document.getelementsbyclassname("menuitems"); function firsthover() { links[0].style.color = "rgb(204, 0, 0)"; /*this avoiding setinterval delay*/ setinterval(function(){ if(links[0].style.color == "rgb(204, 0, 0)") { links[0].style.color = "rgb(235, 153, 153)"; } if(links[0].style.color == "rgb(235, 153, 153)") { links[0].style.color = "rgb(204, 0, 0)"; } },1150); } </script>
the problem is: changes color once.
i tried use hexadecimal color too, doesn't work.
please patient, still novice.
the problem small logical flaw. color change, changes right away.
if first if
statement evaluates true , color set rgb(235, 153, 153)
second if
statement happens true well, , gets checked right after change. color changes other rgb value.
using else if
instead of 2 separate statements fixes this. alternatively place return
in first if statement prevent second being executed after successful change.
if(links[0].style.color == "rgb(204, 0, 0)") { links[0].style.color = "rgb(235, 153, 153)"; } else if(links[0].style.color == "rgb(235, 153, 153)") { links[0].style.color = "rgb(204, 0, 0)"; }
Comments
Post a Comment