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.
|
|
|
|
|
![]() |
||
Looping through objects with variables and strings [Help Please]
|
||
| Notices |
![]() |
|
|
LinkBack | Thread Tools |
|
|||
|
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 |
|
|
|
||||
|
Re: Looping through objects with variables and strings [Help Please]
Your problem is here:
objectIWantToChange = appletName[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]);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. |
![]() |
| Tags |
| variable, strings, loop, javascript, help, arrays, array, variables |
| Thread Tools | |
|
|
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 |