Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript change image when onmouseover and onmouseout

I am a beginner and I am doing a homework and I have a small problem. My task is when onmouseover is on, image1 should be changed to image2. When onmouseout is on, image2 should be changed to image1 Onmouseover works but onmouseout does not work! Here is my code:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>DOM_4</title>
</head>
<body>
<img onmouseover = "changePic1()"onmouseout= "changePic2()" id="myImg"           src="bakground.jpg" width="150" height="200">

<script>
function changePic1() {
document.getElementById("myImg").src = "screen.jpg";
   }
  function changePic22(){
   document.getElementByID("myImg").src = "bakground.jpg";
   }
 </script>
 </body>
</html>

Why my onmouseout does not work? I know there can be better ways to code, but I must use both onmouseover and onmouseout. There should be only HTML and simple javascript codes. (My teacher said so)

like image 323
javaprogrammer Avatar asked Mar 11 '26 15:03

javaprogrammer


1 Answers

Your function name changePic22() is incorrect and you have also syntax error document.getElementByID

Replace you code to this one

function changePic1() {
  document.getElementById("myImg").src = "screen.jpg";
}
function changePic2(){
  document.getElementById("myImg").src = "bakground.jpg";
}
like image 139
Devsullo Avatar answered Mar 13 '26 03:03

Devsullo