Valerie Meunier

Valerie Meunier

  • NA
  • 693
  • 82.9k

how to simplify this?

Sep 2 2022 9:50 PM

Hi

I want to apply onmouseover / out to each of the elements of <li>. I only can use one time the id="a1", so this code works only for the first element. Putting the id in the <di> like <div id="a1"> doesn't work. Thanks for help. V

<div>
        <ul>
            <li> <a id="a1" hef="#">Welcome</a> </li>
            <li> <a hef="#">Contact</a> </li>
            <li> <a hef="#">Info</a> </li>
       </ul>
</div>

    <script type="text/javascript">
        var a = document.getElementById("a1");
        a.onmouseover = txtover
        a.onmouseout = txtout

        function txtover() { a.style.color = "red"; }
        function txtout() { a.style.color = "green"; }
    </script>


Answers (2)