Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JavaScript can anyone tell me whats wrong with my code when I click the pictures

ID: 3844908 • Letter: J

Question

JavaScript can anyone tell me whats wrong with my code when I click the pictures it doesnt open the url i think theyre something wrong with my function open URl


<html>
<head>
<script>
function update(img)
{
document.getElementById('myImg').src=img;
}
   function openURL()
{
var img = document.getElementById('myImg').src;
var filename = img.replace(/^.*[\/]/, '');
if(filename == "1.jpg"){
window.open("http://images.geeknative.com.s3.amazonaws.com/wp-content/uploads/2014/11/hanzo_presskit.png");
}else if (filename == "2.jpg"){
window.open("http://www.geeksandcom.com/wp-content/uploads/2015/06/Overwatch-Bastion.jpg");
       } else if (filename == "3.jpg"){
window.open("http://images.geeknative.com.s3.amazonaws.com/wp-content/uploads/2014/11/pharah_presskit.png");
       } else if (filename == "4.jpg"){
window.open("http://www.gamersbook.com/Portals/0/images/2014/overwatch-character-art-9.jpg");
       } else if (filename == "5.jpg"){
window.open("https://s-media-cache-ak0.pinimg.com/736x/f3/bb/d2/f3bbd2bb8cf7a628d41f301440b6e53f.jpg");
       }
}
</script>
</head>
<body>
<img id="myImg"/><br/>
<img src="1.jpg"/>
<img src="2.jpg"/>
<img src="3.jpg"/>
<img src="4.jpg"/>
<img src="5.jpg"/>

</body>
</html>

Explanation / Answer

<html>
<head>
<script>
function update(img)
{
document.getElementById('myImg').src=img;
}
function openURL()
{
var img = document.getElementById('myImg').src;
var filename = img.replace(/^.*[\/]/, '');
if(filename == "1.jpg"){
window.open("http://images.geeknative.com.s3.amazonaws.com/wp-content/uploads/2014/11/hanzo_presskit.png");
}else if (filename == "2.jpg"){
window.open("http://www.geeksandcom.com/wp-content/uploads/2015/06/Overwatch-Bastion.jpg");
} else if (filename == "3.jpg"){
window.open("http://images.geeknative.com.s3.amazonaws.com/wp-content/uploads/2014/11/pharah_presskit.png");
} else if (filename == "4.jpg"){
window.open("http://www.gamersbook.com/Portals/0/images/2014/overwatch-character-art-9.jpg");
} else if (filename == "5.jpg"){
window.open("https://s-media-cache-ak0.pinimg.com/736x/f3/bb/d2/f3bbd2bb8cf7a628d41f301440b6e53f.jpg");
}
}
</script>
</head>
<body>
<img id="myImg" onmousedown="openURL();"/><br/>
<img src="1.jpg"/>
<img src="2.jpg"/>
<img src="3.jpg"/>
<img src="4.jpg"/>
<img src="5.jpg"/>
</body>
</html>

-> You have missed to call the function.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote