Looping through objects with variables and strings [Help Please]

This is a discussion on "Looping through objects with variables and strings [Help Please]" within the JavaScript Forum section. This forum, and the thread "Looping through objects with variables and strings [Help Please] are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Mar 24th, 2007, 12:40
New Member
Join Date: Mar 2007
Location: Australia
Posts: 7
Thanks: 1
Thanked 1 Time in 1 Post
Looping through objects with variables and strings [Help Please]

Hello everyone,
Sorry I haven’t made a proper introduction to the site yet but I need some assistance. I'm working on fixing some of my old and fairly dodgy coding, and I would greatly appreciate if someone could tell me how to loop through objects

For example

var appletName = new Array(3)
appletName[0] = applet1
appletName[1] = applet2
appletName[2] = applet3

Now if I want to make automate it's self using strings and variables,
var a=1
var b=a-1
appletName[b] = “applet” + a

… and looped it so that a and b increment by 1 each time

Would this work? Because I have found in my previous codes that when ever I attempt at making an object from adding a variable and a string together... it does not work, though this could be due to the fact I didn’t debug, any suggestions?

Here is what I mean (There may be a few other errors, as this is hypothetical code, not the actual code, but basically it is)

... <body onLoad="name()" ...

... <script>
function name(){
if (appletName[b] != 0){
appletName[b] = "applet" + a
objectIWantToChange = appletName[b]
objectIWantToChange.style.left = 300 * b
b++
a++

}
}
}
</script> ...
If someone knows a more effective way of achieving this, can somebody please post a reply.

Last edited by andrewbriscoe87; Mar 24th, 2007 at 13:17. Reason: Realised I Didn't forget a line, I just put one in the wrong place without the appriopriate quotes
Reply With Quote

  #2 (permalink)  
Old Mar 24th, 2007, 16:14
spinal007's Avatar
Moderator
Join Date: Mar 2004
Location: Good Ol'London
Age: 22
Posts: 1,620
Blog Entries: 1
Thanks: 0
Thanked 2 Times in 2 Posts
Send a message via ICQ to spinal007 Send a message via MSN to spinal007 Send a message via Yahoo to spinal007 Send a message via Skype™ to spinal007
Re: Looping through objects with variables and strings [Help Please]

Your problem is here:
objectIWantToChange = appletName[b]
objectIWantToChange.style.left = 300 * b
objectIWantToChange is null, not the object you want to change.
To get the object you want to change, use:
objectIWantToChange=document.getElementById(appletName[b]);
objectIWantToChange.style.left = 300 * b
in which case objectIWantToChange will hold the actual DOM object you want to access.

As for a better way to do this...
Your code suggests the object IDs are appletn where n is a number 1,2,3, etc...
I'd suggest doing this:
... <body onLoad="name()" ...
<script>
function name(){
for(var i=99;i>0;i--){
document.getElementById("applet" + i).style.left = 300 * i;
}
}
// where 99 is the number of objects...
</script> ...


Last Blog Entry: Random String in Javascript (Apr 21st, 2008)

Last edited by spinal007; Mar 24th, 2007 at 16:22.
Reply With Quote
Reply

Tags
variable, strings, loop, javascript, help, arrays, array, variables

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
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Automatic Button 'Looping' for Navigation starrzo JavaScript Forum 2 Apr 4th, 2008 14:38
how to stop flash Cs3 looping animation megb6806 Flash & Multimedia Forum 7 Aug 10th, 2007 02:49
Looping through arrays assgar Starting Out 1 Apr 22nd, 2007 18:43
PHP Looping script peterboy PHP Forum 3 Mar 10th, 2007 00:04
looping background music gwx03 Flash & Multimedia Forum 6 Sep 7th, 2003 03:07


All times are GMT. The time now is 10:29.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43