Search code examples
javascripthtmlonmouseover

Change image on mouse over in javascript


I'm building a webpag in html. The image must change when I mouse over a div, but it does nothing. Anyone any idea?

<!doctype html>
<html>
    <head>
          <meta charset ="UTF-8"/>
          <title>Oef 19.5</title>
          <link href="19_5.css" rel="stylesheet"/>
          <script>

          function kleuren(veld)
          {
            var tshirt = document.getElementsByClassName("shirt");

            tshirt.src=veld.id+".jpg";
          }


          </script>
    </head>
<body>
    <h1>Pick a color</h1>
    <p><img class="shirt" src="gray.jpg"></p>

    <div id="pink" class="roze" onmouseover="kleuren(this)"></div>
    <div id="blue" class="blauw" onmouseover="kleuren(this)"></div>
    <div id="brown" class="bruin" onmouseover="kleuren(this)"></div>

</body>
</html>

Solution

  • var tshirt = document.getElementsByClassName("shirt"); returns a collection of elements so access it like

    tshirt[0].src=veld.id+".jpg"; //Since you have only one element with that class. If you have more, iterate over them