/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Using ‘i’ for a variable variable

OK, everyone… I’m brand new to this forum, but here’s my big question:

What is the appropriate syntax for what I’m trying to do?

[CODE]document.[“form” + i].item_number.value = [clientID + i];[/CODE]

This is an excerpt from a for loop where i = 12. I have 12 forms that I’m addressing. I need to give each form’s item_number an increasing value as well. I’m making a Paypal shopping cart.

Here’s the whole function. Everything works except the last line. Any ideas on the correct syntax, guys?

[CODE]
var clientID = “bassett”
function preloadImages() {
for(var i = 1; i <= 12; i++)
{
window[clientID + i] = new Image();
window[clientID + i].src = [“../client_proofs/” + clientID + “/” + clientID + i + “.jpg”]
window[clientID + “_thumb” + i] = new Image();
window[clientID + “_thumb” + i].src = [“../client_proofs/” + clientID + “/” + clientID + “_thumb” + i + “.jpg”]
document.[“form” + i].item_number.value = [clientID + i];
}
}[/CODE]

I’ve been trying to figure this out for over 3 hours now! Thanks in advance!

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@ZeroKilledApr 11.2009 — document.["form" + i].item_number.value = [b][clientID + i][/b];
the bold code is what is giving you problem. it mean that you are using [b]clientID[/b] variable and adding the index count, but because is inside brackets, you are creating an array. i guess you meant to do

document.["form" + i].item_number.value = clientID + i;

notice there is no bracket. is valid if clientID variable is defined.
Copy linkTweet thisAlerts:
@Declan1991Apr 11.2009 — A bit of advise first, you are creating 24 global variables, which besides being a bad idea, it's harder to maintain. And not only have you the square brackets problem already pointed out, but it's document["form" + i], no full stop.
<i>
</i>var clientID = "bassett", images = [], thumbnails = [];
function preloadImages() {
for(var i = 1; i &lt;= 12; i++)
{
images[i] = new Image();
images[i].src = "../client_proofs/" + clientID + "/" + clientID + i + ".jpg";
thumbnails[i] = new Image();
thumbnails[i].src = "../client_proofs/" + clientID + "/" + clientID + "_thumb" + i + ".jpg";
document["form" + i].item_number.value = clientID + i;
}
}
Also, in the future, I'd suggest that you start indexing at 0, and work up to 11, because that's normally the way JavaScript counts. And for more information on arrays, [url=http://www.w3schools.com/JS/js_obj_array.asp]W3Schools[/url] will oblige.
Copy linkTweet thisAlerts:
@thumper87authorApr 12.2009 — So, this works:
[CODE]document["form" + "1"].item_number.value = clientID + i;[/CODE]

But this doesn't work:

[CODE]document["form" + i].item_number.value = clientID + i;[/CODE]

The one and only difference being the "1" instead of the i in the form1 variable. I've debugged it down to this point. Now, after different combinations of " ", ' ', ( ), [ ] and even { }, I'm stuck. The other instances of the i variable in the code work just fine! What's wrong with this one???
Copy linkTweet thisAlerts:
@ZeroKilledApr 12.2009 — has you made the changes that [b]Declan1991[/b] pointed? is there more than one form in the HTML code? please, post the relevant html code. for some reason i'm assuming there is one form and multiple item_number field.

Now, after different combinations of " ", ' ', ( ), [ ] and even { }, I'm stuck.[/quote]
at least has you learned the basis of javascript syntax? each of them have special meaning for the language.
Copy linkTweet thisAlerts:
@thumper87authorApr 12.2009 — Ugh! I feel like such an idiot! I hadn't made all my forms yet, so I was incidentally targeting forms that weren't there, which threw an error and stopped the script in it's tracks. All worked out now!

I ended up going with this:
[CODE]function preloadImages() {
for(var i = 1; i <= 12; i++)
{
window[clientID + i] = new Image();
window[clientID + i].src = ["../client_proofs/" + clientID + "/" + clientID + i + ".jpg"];
window[clientID + "_thumb" + i] = new Image();
window[clientID + "_thumb" + i].src = ["../client_proofs/" + clientID + "/" + clientID + "_thumb" + i + ".jpg"];
document.getElementById("form" + i).item_number.value = clientID +i;
document.getElementById("form" + i).item_name.value = eventName + i;
}[/CODE]


Thanks for your help, guys! I'm pretty new to Javascript. Most of my experience is in Actionscript. A lot of that translates over, but some of the syntax and protocol is different enough to confuse me.
Copy linkTweet thisAlerts:
@Declan1991Apr 12.2009 — That works? I never knew that src would accept an array as a value.
×

Success!

Help @thumper87 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...