JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 16th, 2005, 06:12 PM
MikeRed MikeRed is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 22 MikeRed User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 12 m 42 sec
Reputation Power: 0
Question about Image swapping within arrays

hello, I'm trying to create a project, and part of it includes using arrays with image swapping. Here's simplified code to show what I'm trying to do.


Code:
<script>

var imgArray = new Array(2);
imgArray[0] = new Image();
imgArray[0].src = "blue.gif";


function swapImage(){
document[imgArray[0]].src = "1.gif";
}


</script>

<img src="blue.gif" onClick="swapImage();">


and the error i get is:

Error: document[imgArray[0]] has no properties on line 9 (the error is from the firefox console.)

Also, the images don't display at all in internet explorer...hrm

Anyways, thanks for all the help you give,

Mike.

Reply With Quote
  #2  
Old February 16th, 2005, 07:03 PM
Saint Jude Saint Jude is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 455 Saint Jude User rank is Lance Corporal (50 - 100 Reputation Level)Saint Jude User rank is Lance Corporal (50 - 100 Reputation Level)Saint Jude User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 2 Days 3 h 4 m 22 sec
Reputation Power: 9
Send a message via Yahoo to Saint Jude
1. imgArray[0] holds an image (ref), so document[imgArray[0]] will not be useful. (very old syntax too).

2. The image element in the document is entirely unrelated to the image object created in the script.

Images created with the Image constructor are usually purely for the purposes of preloading.
They needn't ever be referenced in the script again. If part of an array, they might be used as a convenient
way of grabbing a source string (alt. ver. below).

You won't see scripts where such image objects become a part of the document, although it is possible to insert them.

You want this (or like it):

PHP Code:
<script>
 
var 
imgArray = new Array(2);
imgArray[0] = new Image();
imgArray[0].src "1.gif";
 
 
function 
swapImage(){
    
document.getElementById(img0).src "1.gif";
    
// alt. // document.getElementById(img0).src = imgArray[0].src;
}
</script>
 
<img id="img0" src="blue.gif" onClick="swapImage();"> 

Reply With Quote
  #3  
Old February 16th, 2005, 07:11 PM
Glen Rowell's Avatar
Glen Rowell Glen Rowell is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Japan
Posts: 410 Glen Rowell User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 13 h 34 m 52 sec
Reputation Power: 9
Is it the same for JavaScript?

Reply With Quote
  #4  
Old February 16th, 2005, 09:33 PM
Kravvitz's Avatar
Kravvitz Kravvitz is offline
CSS & JS/DOM Adept
Dev Shed God 30th Plane (19500 - 19999 posts)
 
Join Date: Jul 2004
Location: USA
Posts: 19,835 Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level) 
Time spent in forums: 6 Months 1 Day 22 h 11 m
Reputation Power: 4192
Glen, what are you asking?
__________________
Spreading knowledge, one newbie at a time. I'm available for hire at Dynamic Site Solutions.

Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

Remember people spend most of their time on other people's sites (so don't violate web design conventions).

Reply With Quote
  #5  
Old February 17th, 2005, 01:45 AM
MikeRed MikeRed is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 22 MikeRed User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 12 m 42 sec
Reputation Power: 0
You answered my question thanks!

Reply With Quote
  #6  
Old February 17th, 2005, 03:40 AM
Saint Jude Saint Jude is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 455 Saint Jude User rank is Lance Corporal (50 - 100 Reputation Level)Saint Jude User rank is Lance Corporal (50 - 100 Reputation Level)Saint Jude User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 2 Days 3 h 4 m 22 sec
Reputation Power: 9
Send a message via Yahoo to Saint Jude
<ERROR>

..but I didn't quote the strings in the getElementById calls.

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Question about Image swapping within arrays

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap