Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Swap Image Button OnClick not OnMouseOver 2

Status
Not open for further replies.

soonkhoo

Programmer
Dec 8, 2000
133
MY
Hi,

I wonder if it's possible to change the image A button to image B button on click then if the image B button is clicked again, it will change back to image A? Also, for each image, it has it's on function as well. Thanks.

Soon
 
Yes, just use OnClick instead. To enable the button to know whether it is down or up, you will have to set a variable.

...

function imageSwap(){
if(!document.myImg.isOn){
swap to other image, and set isOn to true}

else if(document.myImg.isOn){
swap back and set isOn to false}


}
...
<a href=&quot;javascript:imageSwap()&quot;><img src=&quot;...&quot; name=&quot;myImg&quot; isOn=false>
</a>

Here I have added a variable to the image simply by writing it inside the img tag, I am not sure if this will cause probs somewhere, but if so you will need to create one using prototype. It worked for me though. Access it in the usual way:

document.imageName.isOn


NOTE: to enable these things for NN, you will need to make it a link like I have done. NN will not read events from an img. This is why I have done this above.


href=&quot;javascript:function&quot; makes the link call a function instead of acting as a link
&quot;Alright whatever man, I'll hook up the hair, but I aint touchin the ring...Cause I'm still a pla--yer&quot;
 
Hi bangers,

I understand your concept... but I don't understand how you swap the images and to set the isOn to true. Do you do all this in the <head> tag?

And in the <body> tag, do you type in this way?
<a href=&quot;javascript:imageSwap()&quot;>

or do you include the image src after the href link.
OR do you include the image src in the function?

Soon
 
Can you include function within a function?? I already have the swapimage function. Or do I need to create a code to swap images within the imageSwap()? Can anyone help me?

 
just call another function formt he original

function tacoBell()
{
taco++
addSourCream()
}

function addSourCream()
{
sourcream++
} jared@aauser.com
 
In bangers code,

function imageSwap(){
if(!document.myImg.isOn){
How do you swap to other image here? and set to True?

else if(document.myImg.isOn){
swap back and set isOn to false}


}
...
<a href=&quot;javascript:imageSwap()&quot;><img src=&quot;...&quot; name=&quot;myImg&quot; isOn=false></a>
 
Oh I'm sorry, I thought you had that code sorted. The concept is, that you have an image, and it's src property holds the source for the data it will display.

By changing the src, it will display a different image, while keeping all other attributes the same; name, isOff etc. So the image remains referencable from the same name.

So supposing we have an image called myImage, and it has an image src called &quot;ButtonImage.gif&quot;. We are going to swap this for another one, or back, depending on some conditions:

<head>
<script language=&quot;JavaScript&quot;>
var myImageOn = new Image();
myImageOn.src = 'Button_On_Image.gif';

function imageSwap(){
if(document.MyImage.isOff){
document.myImage.isOff = false;
document.myImage.src = myImageOn.src;
}

else if(!document.bang.isOff){
document.myImage.isOff = true;
document.myImage.src=&quot;ButtonImage.gif&quot;;
}
}
</script>

</head>

<body bgColor=&quot;black&quot;>

<a href=&quot;javascript:imageSwap()&quot;>
<img border=&quot;0&quot; name=&quot;myImage&quot; isOff=true src=&quot;ButtonImage.gif&quot; width=&quot;60&quot; height=&quot;60&quot; ></a>

</body>



Note:
We have switched the logic and used isOff now because I found the effect did not work on the first click of the button, but did on subsequent clicks.
This is something to do with setting the variable to false when initialising. Now it works right form word go.

We have used the Image() constructor to hold an image.An image cannot be displayed purely using the constructor, only by giving it's src to an existing one.

By using an internal variable it will make it easier toi modify the function to cope with multiple changing images.

I hope this has clarified it a bit.
Let me know if you want to modify it to accept multiple images.This will involve storing images in Arrays.
-Ben &quot;Alright whatever man, I'll hook up the hair, but I aint touchin the ring...Cause I'm still a pla--yer&quot;
 
This is COOL!!! Thanks! But one thing though, now that I have your code to swap images I can't get my buttons to work for my mp3 player. I have a script written in VBscript and the Play and Stop button has its own name:
BtnPlay and BtnStop.

In your code, it is written keeping the name of the src constant. How then can I change the name?
 
Ben,

Here is the source code for the WMP. It's Half VBscript Half Javascript. Is it possible to call a function from another language to another function?? Or just forget about the VBscript and use MediaPlayer1.Play to play? If so, where??

--Soon(desperate)


<HTML>
<HEAD><TITLE>Windows Media Player Test</TITLE>
<SCRIPT LANGUAGE=&quot;VBScript&quot;>
<!--
Sub BtnPlay_OnClick
MediaPlayer1.Play
End Sub

Sub BtnStop_OnClick
MediaPlayer1.Stop
MediaPlayer1.CurrentPosition = 0
End Sub
-->
</SCRIPT>

<script language=&quot;JavaScript&quot;>
<!--

var myImageOn = new Image();
myImageOn.src = 'Images/play.gif';

function imageSwap(){
if(document.BtnStop.isOff){
document.BtnStop.isOff = false;
document.BtnStop.src = myImageOn.src;
}
else if(!document.BtnStop.isOff){
document.BtnStop.isOff = true;
document.BtnStop.src=&quot;Images/stop-up.gif&quot;;
}
}

//-->
</script>
</HEAD>
<BODY bgcolor=&quot;#EEEEEE&quot;>
<BR>

<object id=&quot;MediaPlayer1&quot; width=0 height=0
classid=&quot;CLSID:22D6f312-B0F6-11D0-94AB-0080C74C7E95&quot;
codebase=&quot; nsmp2inf.cab#Version=6,4,5,715&quot;
standby=&quot;Loading Microsoft® Windows Media™ Player components...&quot;
type=&quot;application/x-oleobject&quot; align=&quot;middle&quot;>
<param name=&quot;FileName&quot; value=&quot;C:\My Music\Bosson - One in a Million (From Miss Congeniality Soundtrack).mp3&quot;>
<param name=&quot;ShowControls&quot; value=&quot;False&quot;>
<param name=&quot;AutoRewind&quot; value=&quot;True&quot;>
<param name=&quot;AutoStart&quot; value=&quot;True&quot;>
<embed name=&quot;MediaPlayer1&quot; width=&quot;27&quot; height=&quot;17&quot; filename=&quot;C:\My Music\Bosson - One in a Million (From Miss Congeniality Soundtrack).mp3&quot; showcontrols=&quot;False&quot; autorewind=&quot;True&quot; autostart=&quot;True&quot; src=&quot;C:\My Music\Bosson - One in a Million (From Miss Congeniality Soundtrack).mp3&quot; align=&quot;middle&quot;>
</embed>
</object>
<a href=&quot;javascript:imageSwap()&quot;><img border=&quot;0&quot; name=&quot;BtnStop&quot; isOff=true src=&quot;Images/stop-up.gif&quot;></a>
<br><br><br><br>

<img src=&quot;Images/play.gif&quot; width=&quot;27&quot; height=&quot;23&quot; name=&quot;BtnPlay&quot; value=&quot;Play&quot; border=&quot;0&quot;>
<img src=&quot;Images/stop-up.gif&quot; width=&quot;27&quot; height=&quot;23&quot; NAME=&quot;BtnStop&quot; VALUE=&quot;Stop&quot;><br>
</BODY>
</HTML>
 
I already saw that thread... but I still don't understand in terms of that swapping images and the collaboration with the player controls...

Soon
 
Those are two completely unrelated tasks. OnClick, call one function to swap images and another to control your media player.
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
I understand this is a completely different task... so then how do I create functions for my swapping image buttons?

Soon
 
Is there a way then when the image is swapped, that means onclick, another function executes? Say, I have a WMP playing onload, clicking on the stop button would stop the music playing and the stop image is swaped to a play button, vice versa.?

Soon
 
<a href=&quot;javascript:stop_player()&quot;><img src=&quot;stop.gif&quot; name=&quot;stop&quot;></a>

<script>
function stop_player()
{
swap(stop);
Player.stop();
}

function swap(img)
{
The code you discussed with bangers above.
}
</script>
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
hmmm for some reason, it didn't work... what is that var img?? Anyway, I thought of another way of swapping the images.... what if I use <div> tag instead? That way I have 2 layers overlapping each other. 1st layer has the stop.gif and the stop function to play the audio. After clicking on the stop button, the layer hides and the 2nd layer shows with the Play button. Then when clicking on the play button, the 2nd layer hides and the 1st layer shows...

Is it possible?? I'm using Dreamweaver, so it's fairly easy for me to layout the layer..
 
Don't use layers, that's silly. Here's a more fleshed-out example:

<head>
<script>
var stop_on = new Image();
stop_on.src = &quot;stop_on.gif&quot;;
var stop_off = new Image();
stop_off.src = &quot;stop_off.gif&quot;;
var play_on = new Image();
play_on.src = &quot;play_on.gif&quot;;
var play_off = new Image();
play_off.src = &quot;play_off.gif&quot;;
var pause_on = new Image();
pause_on.src = &quot;pause_on.gif&quot;;
var pause_off = new Image();
pause_off.src = &quot;pause_off.gif&quot;;

function swap(img,which)
{
eval(&quot;document.&quot;+img+&quot;.src=&quot;+img+&quot;_&quot;+which+&quot;.src&quot;);
}

function stop_player()
{
swap(&quot;stop&quot;,&quot;on&quot;);
swap(&quot;play&quot;,&quot;off&quot;);
swap(&quot;pause&quot;,&quot;off&quot;);

Player.stop();
}

function pause_player()
{
swap(&quot;stop&quot;,&quot;off&quot;);
swap(&quot;play&quot;,&quot;off&quot;);
swap(&quot;pause&quot;,&quot;on&quot;);

Player.pause();
}

function play_player()
{
swap(&quot;stop&quot;,&quot;off&quot;);
swap(&quot;play&quot;,&quot;on&quot;);
swap(&quot;pause&quot;,&quot;off&quot;);

Player.stop();
}
</script>
</head>

<body>
<a href=&quot;javascript:stop_player()&quot;><img src=&quot;stop_off.gif&quot; name=&quot;stop&quot;></a>
<a href=&quot;javascript:pause_player()&quot;><img src=&quot;pause_off.gif&quot; name=&quot;pause&quot;></a>
<a href=&quot;javascript:play_player()&quot;><img src=&quot;play_off.gif&quot; name=&quot;play&quot;></a>
</body>
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
That should be Player.play() in the play_player() function :)

I've made a color-coded version of the swap function in faq215-373 to help distinguish the variables from the strings.
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
Yes... but your code has more than 1 button... I need only 1 button. Is possible for you to take out the 2 buttons.. leave 1 there to swap. Since your function, say, stop-on button, when clicked it swaps with stop-off button. Therefore, can you replace your stop-off button with play-on button instead? Just like what bangers did.

Regards,
Soon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top