I have tried to create a simple JavaScript which was supposed to switch images when links are clicked. This is the code of the whole site (It’s not much, but it was only meant to be a test):
<html>
<head>
<title>Slideshow</title>
<style type="text/css">
#wrapper {
width: 800px;
height: 250px;
margin: 75px auto auto auto;
}
</style>
<script type="text/javascript">
function changeImg(x) {
document.getElementById(presentationImg).src='x';
}
</script>
</head>
<body>
<div id="wrapper">
<div id="mainnav">
<ul>
<li><a href="#" onClick="changeImg(design.jpg)">Design</a></li>
<li><a href="#" onClick="changeImg(realisation.jpg)">Realisation</a></li>
<li><a href="#" onClick="changeImg(deployment.jpg)">Deployment</a></li>
</ul>
</div>
<div id="presentation">
<img id="presentationImg" src="design.jpg" />
</div>
</div>
</body>
I was pretty sure it would work, but it doesn’t, and I have no idea why… does anyone know why, and what the solution is?
Get rid of the quotes around
xin your function, and quote the element ID instead:You will also need to quote the string you pass in to your function:
The way you have it currently,
presentationImgwill be undefined, as you haven’t declared a variable of that name. To make it work the way you had it originally you could do this: