PDA

View Full Version : onclick change images



emanuelle
09-15-2007, 06:27 PM
looking for a code:
when click on image1: image1 changes to image2
when click on image2: image2 changes back to image1

must work in firefox

coothead
09-15-2007, 07:07 PM
Hi there emanuelle,

try it like this example...

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<style type="text/css">
div {
text-align:center;
}
#pic{
border:3px double #999;
}
</style>

<script type="text/javascript">
var c=0;
window.onload=function() {
document.getElementById('pic').onclick=function() {
swapImage();
}
}
function swapImage() {
obj=document.getElementById('pic');
if(c==1) {
obj.src='http://mysite.orange.co.uk/azygous/images/apple0.jpg';
c=0;
}
else {
obj.src='http://mysite.orange.co.uk/azygous/images/banana.jpg';
c=1;
}
}
</script>

</head>
<body>

<div>
<img id="pic" src="images/apple0.jpg" alt="apple"/>
</div>

</body>
</html>
coothead

emanuelle
09-17-2007, 05:55 PM
ok txs
now what if I have like 10 images on my page that change when onclick. can I make something dynamic?