...

View Full Version : Help for simple change on click



danishviking
11-01-2012, 02:20 PM
I suck at javascript.
All I wanted to do is this:

I have a jpg. When I click that image I want it to change to a different jpg for 1 second and play a sound. After the click I want everything to turn back to normal.

This should be really easy right?

optimizer123
11-01-2012, 02:22 PM
This should be really easy right?

Yes it is.

Try this:


<script type="text/javascript">
imgs=Array("img1.jpg","pic2.jpg","another.jpg","whee.gif","whatever.png");
var x=0;

function change() {
document.getElementById("bob").src=imgs[++x];

if (x==5) {
x=-1;
}
}
</script>

<img src="img1.jpg" id="bob" alt="" onmousedown="change()">

^This will change the picture into another picture (as in Image "img1.jpg" will change to image "pic2.jpg" and so on.)

danishviking
11-01-2012, 02:31 PM
Yes it is.

Try this:


<script type="text/javascript">
imgs=Array("img1.jpg","pic2.jpg","another.jpg","whee.gif","whatever.png");
var x=0;

function change() {
document.getElementById("bob").src=imgs[++x];

if (x==5) {
x=-1;
}
}
</script>

<img src="img1.jpg" id="bob" alt="" onmousedown="change()">

^This will change the picture into another picture (as in Image "img1.jpg" will change to image "pic2.jpg" and so on.)

Hey Optimizer
Thanks for getting back so quickly.
Does the above script turn From jpg1 to jpg 2 on click and then back to jpg 1 after the click? Also does it play a sound on click?
Thanks for taking time to help me out, much appreciated :thumbsup:

optimizer123
11-01-2012, 03:19 PM
Does the above script turn From jpg1 to jpg 2 on click and then back to jpg 1 after the click? Also does it play a sound on click?


imgs=Array("img1.jpg","pic2.jpg","another.jpg","whee.gif","whatever.png");

^ this shows you that when you click on the image it will change to pic2.jpg.. when you click on that picture it will change to another.jpg.. if you only want 2 pictures you should do it like this:


imgs=Array("img1.jpg","pic2.jpg");

Do you want it to be that when you hover your mouse over the image it plays a sound? or when you click on it?

danishviking
11-01-2012, 03:29 PM
I only have 2 images.

Let's say the image is of a bird (jpg1).
When I click on the bird the beak opens (ie. jpg 2) and a small tweet sound comes out. Only on the click. When I release the click everything goes back to normal ie. jpg 1

Does that make sense? Im terrible at explaining :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum