Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-05-2009, 12:30 PM   PM User | #1
happyandrea
New to the CF scene

 
Join Date: Feb 2009
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
happyandrea is an unknown quantity at this point
Generic swap image

Hello all,

i am trying to do a script that let me change the src of an image when someone click on it. When the second image (a bigger image) appear and the user hit the image with a second click, appears the first image. And so on.

This is what i've done:

Code:
var newImage;

function swapImage(nomeid, newImage) { 
if(document.getElementById('nomeid').src == newImage) { 
document.getElementById('nomeid').src = "images/"+nomeid+"_big.jpg"; 
}else{ 
document.getElementById('nomeid').src = newImage; 
} 
}
and

Quote:
<IMG id="image1" SRC="images/image1.jpg" onClick="swapImage(image1,'images/image1.jpg')" />
But it dosn't work.
I need a generic script to be used easily in a page with a large number of images... and as u can see I don't know JS so much

Thanks for any hint...

Last edited by happyandrea; 02-05-2009 at 12:33 PM..
happyandrea is offline   Reply With Quote
Old 02-05-2009, 01:53 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,038
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Search Google for javascript rollover which will bring up lots of scripts.


Quizmaster: What "R" was Hilary Clinton's maiden name?
Contestant: Er, um . .. is it Rottweiler?
Philip M is online now   Reply With Quote
Old 02-05-2009, 02:06 PM   PM User | #3
happyandrea
New to the CF scene

 
Join Date: Feb 2009
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
happyandrea is an unknown quantity at this point
Already tried, but i cannot find what i'm looking for
happyandrea is offline   Reply With Quote
Old 02-05-2009, 02:10 PM   PM User | #4
freedom_razor
Regular Coder

 
Join Date: Dec 2008
Location: Tannhäuser Gate
Posts: 286
Thanks: 7
Thanked 58 Times in 57 Posts
freedom_razor is an unknown quantity at this point
Here you go:
Code:
function swapImage(nomeid, newImage) { 
if(document.getElementById(nomeid).getAttribute('src') == newImage) { 
document.getElementById(nomeid).src = "images/"+nomeid+"_big.jpg"; 
}else{ 
document.getElementById(nomeid).src = newImage; 
} 
}
And your IMG tag:
Code:
<img id="image1" src="images/image1.jpg" onClick="swapImage('image1','images/image1.jpg')" />
freedom_razor is offline   Reply With Quote
Users who have thanked freedom_razor for this post:
happyandrea (02-05-2009)
Old 02-05-2009, 02:49 PM   PM User | #5
happyandrea
New to the CF scene

 
Join Date: Feb 2009
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
happyandrea is an unknown quantity at this point
It's perfect... thanks a lot man!

So was the "getAttribute('src')" missing... I'll go study some JS
happyandrea is offline   Reply With Quote
Old 02-05-2009, 03:12 PM   PM User | #6
freedom_razor
Regular Coder

 
Join Date: Dec 2008
Location: Tannhäuser Gate
Posts: 286
Thanks: 7
Thanked 58 Times in 57 Posts
freedom_razor is an unknown quantity at this point
Well, there were few errors there, you can compare codes and see them [too many '' ]. And this document.getElementById('nomeid').src == newImage wasn't working, because left side returns full path to image, and you were comparing it to relative path, so it was never the same.
freedom_razor is offline   Reply With Quote
Reply

Bookmarks

Tags
image, javascript, swap

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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


All times are GMT +1. The time now is 01:16 PM.


Advertisement
Log in to turn off these ads.