Go Back   HostGator Peer Support Forums > HostGator Peer Support Forums > Web Hosting Services

Notices

Reply
 
Thread Tools
  #1  
Old 06-11-2008, 10:38 AM
andrewtayloruk andrewtayloruk is offline
Hatchling Croc
 
Join Date: Jan 2008
Posts: 30
Default Javascript onClick Image Change

Hi, i don't know if this is the correct place for this but here goes.

I have an image inside an <a> tag and need it to change when clicked. Most of the examples i've seen will change once clicked but don't change back to the original image when clicked again.

I don't suppose any of you Javascript guru's could knock something up that will do this?

Any help would be appreciated.

Thanks,
Andrew
Reply With Quote
  #2  
Old 06-11-2008, 12:17 PM
brad brad is offline
Baby Croc
 
Join Date: Jan 2008
Posts: 89
Default Re: Javascript onClick Image Change

i don't know JS well enough to ramble something off the top of my head, but it seems pretty straight forward if you have code to change it when clicked. just keep track with a variable (if it's only 2 images, it could even be a boolean) and hack the code to change the desired image based on what the current value of the variable is.

something like

image = ((image == 1)?0:1);

then display 'image'+image to show either image1 or image0

there may be a more eloquent way to do it, but that's usually how i do these sorts of things.
Reply With Quote
  #3  
Old 06-11-2008, 01:25 PM
atlan atlan is offline
Swamp Croc
 
Join Date: Oct 2007
Posts: 381
Default Re: Javascript onClick Image Change

Not much Javascript here either.

do an onmouseout= ??????

function mouseOut()
{
document.b1.src ="imagename.jpg .gif whatever";
}

and add this within your <a> tag

onmouseout="mouseOut()">

Last edited by atlan; 06-11-2008 at 01:28 PM.
Reply With Quote
  #4  
Old 06-11-2008, 06:02 PM
slapshotw's Avatar
slapshotw slapshotw is offline
Veteran Croc
 
Join Date: Jun 2006
Posts: 5,164
Default Re: Javascript onClick Image Change

In the header, try:
Code:
var img1 = image1.jpg;
var img2 = image2.jpg;
var imageChoice = 1;

function imageChange(element) {

if (imageChoice == 1) {
element.src == img2;
imageChoice = 2;
}
else if (imageChoice == 2) {
element.src == img1;
imageChoice = 1;
}
And for the image, use:
Code:
<img src="image1.jpg" onclick="imageChange(this);">
__________________
Follow me on Twitter! http://twitter.com/mrw
Reply With Quote
  #5  
Old 06-12-2008, 08:29 AM
brad brad is offline
Baby Croc
 
Join Date: Jan 2008
Posts: 89
Default Re: Javascript onClick Image Change

combined with my code, it'd be a bit cleaner. at the least, that shouldn't an else if as it'll make the second if be evaluated which is unnecessary. else should be enough alone.
Reply With Quote
  #6  
Old 06-13-2008, 04:06 PM
slapshotw's Avatar
slapshotw slapshotw is offline
Veteran Croc
 
Join Date: Jun 2006
Posts: 5,164
Default Re: Javascript onClick Image Change

Quote:
Originally Posted by brad View Post
that shouldn't an else if as it'll make the second if be evaluated which is unnecessary
I like to do it because it makes it easier for me to follow with my own code, and if it doesn't work, it could mean something else is wrong.
__________________
Follow me on Twitter! http://twitter.com/mrw
Reply With Quote
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Javascript Popup esl Webhosting 10 10-03-2007 06:31 PM
Javascript Form Help tomowa Off-Topic 2 03-15-2007 11:26 AM
Javascript suggestions britbob Web Hosting Services 0 03-15-2005 10:18 PM
javascript and other extensions Anonymous Pre-Sales Questions 1 07-08-2004 01:15 AM

All times are GMT -5. The time now is 09:54 AM.