New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
Web Design and Hosting Creating sites, scripting, and hosting discussions.

Javascript Image Change


Post Reply New Thread Subscribe

 
Thread Tools
FinnIII

 
Member Since: Jan 17, 2008
Posts: 16
FinnIII is on a distinguished road
Mac Specs: MacBook Pro: 2.16 GHz 2GB ram

FinnIII is offline
I have found a code that changes the background image of a page. Is it possible to adapt it to change an image on the page?

Code:
<script language="JavaScript">
<!--

// Copyright 2001 by www.CodeBelly.com
// Please do *not* remove this notice.

var backImage = new Array();

backImage[0] = "../gallery_pages/images/stupid/full-32 King Stupid Main Set.JPG";
backImage[1] = "../gallery_pages/images/stupid/full-33 King Stupid Control Desk.jpg";
backImage[2] = "../gallery_pages/images/stupid/full-34 King Stupid Main Set Rev..JPG";

function changeBGImage(whichImage){
if (document.body){
document.body.background = backImage[whichImage];
}
}

//-->
</script>
Can you help?
QUOTE Thanks
Thyamine

 
Thyamine's Avatar
 
Member Since: Apr 23, 2007
Location: Coatesville, PA
Posts: 377
Thyamine will become famous soon enough
Mac Specs: MBP 15", 2.33 GHz, 2Gb

Thyamine is offline
There are a lot of ways to change the image on a page. This is simply using javascript to refer to the document background object. You can do the same thing with an image by giving it an ID (a name), and then use Javascript to find it and change the src.

I couldn't find the right tutorial I wanted to link to, but you can certainly google it and find one that makes sense to you.
QUOTE Thanks
knightlie

 
knightlie's Avatar
 
Member Since: Mar 22, 2007
Location: UK
Posts: 1,463
knightlie is just really niceknightlie is just really niceknightlie is just really niceknightlie is just really nice
Mac Specs: Lenovo Z560 Hackintosh -:- '06 iMac -:- iPod Touch 2ndGen

knightlie is offline
Code:
var img;

img = document.getElementById('imageID');
img.src = 'http://website.com/image.jpg';
That's off the top of my head, but it's something similar. The IMG tag needs an id="imageID" for this to work:

Code:
<img id="imageID" src="image.jpg" />
You can also pre-load the new image into a variable and assign it later, but the code is slightly different. As Thyamine said there are bazillions of tutorials on the web for doing this

[URL="http://beadia.net"]Beadia[/URL - Jewelry Business Management Software]
I judge you when you use poor grammar.
QUOTE Thanks
Thyamine

 
Thyamine's Avatar
 
Member Since: Apr 23, 2007
Location: Coatesville, PA
Posts: 377
Thyamine will become famous soon enough
Mac Specs: MBP 15", 2.33 GHz, 2Gb

Thyamine is offline
What Knightlie has entered is pretty much what you need.

One thing to note, case sensitivity can be important, and I have a tendency to type getElementByID (capital ID) instead of getElementById, and it always takes me forever to track down where the heck the javascript error I'm getting is from.
QUOTE Thanks
giulio

 
giulio's Avatar
 
Member Since: Mar 19, 2007
Location: NY USA
Posts: 1,813
giulio is just really nicegiulio is just really nicegiulio is just really nicegiulio is just really nice
Mac Specs: iMac 5.1 | iMac 7.1 | iPod Touch | iPod Nano

giulio is offline
Less code:

Quote:
document.getElementByID('yourimageID').src='/media/image.jpg';
Or if you're using prototype/scriptaculous, even less:

Quote:
$('yourimageID').src='/media/image.jpg';

schweb This has to be one of the best threads ever in MF history...
Cherokee This gets my vote for most innovative thread on Mac-Forums... ever.
QUOTE Thanks
FinnIII

 
Member Since: Jan 17, 2008
Posts: 16
FinnIII is on a distinguished road
Mac Specs: MacBook Pro: 2.16 GHz 2GB ram

FinnIII is offline
I kind of get the logic, but It isn't working for me.

In the head, I have:
Code:
<script language="JavaScript">
document.getElementByID('imageID').src='../gallery_pages/images/stupid/full-32.JPG';  
</script>
And the Body:
Code:
<img id="imageID" src="../gallery_pages/images/stupid/full-32.JPG"/>
and
Code:
<a href="javascript:'imageID'.src='../gallery_pages/images/stupid/full-33 King Stupid Control Desk.jpg'"><img src="../gallery_pages/images/stupid/thumbs15/thumb-full-33" border="0" /></a>
What needs correcting? Oh, and you have been a big help so far
QUOTE Thanks
Thyamine

 
Thyamine's Avatar
 
Member Since: Apr 23, 2007
Location: Coatesville, PA
Posts: 377
Thyamine will become famous soon enough
Mac Specs: MBP 15", 2.33 GHz, 2Gb

Thyamine is offline
getElementByID should be getElementById (lowercase d).
QUOTE Thanks

Post Reply New Thread Subscribe


« Flash CS3 scrollbar does not work in all frames | Mac OS = no JAVA? »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread
Thread Starter
Forum
Replies
Last Post
Change What An Image Opens With XMARLTONX OS X - Operating System 3 05-16-2007 01:32 AM
Image Files On Desktop Mummyscurse Switcher Hangout 2 09-13-2006 02:16 PM
burning an OSX CD using a .dmg (mac image) file on a PC bitt3n OS X - Operating System 4 08-09-2006 12:48 PM
Changing DW's dropdown nav background from solid color to an image? Morgan19 Web Design and Hosting 4 02-01-2006 02:48 PM

All times are GMT -4. The time now is 07:52 PM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
X

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?