/    Sign up×
Community /Pin to ProfileBookmark

Array to define var?

Hi

I am trying to work on a long form for calculating house contents for insurance purposes. As you can see below, my code is rediculously long and it also stops working when it gets to var a10 for some reason. ?
Is there an easier way to do this using an array? Hopefully you can see what I am trying to achieve. Thank you in advance for any asissitance. ?

function addrow(){

// Add the new items into the final total column

//assigns the current value of the input box for each row to a new variable
var a001 = (document.contents.a1.value*1);
var a002 = (document.contents.a2.value*1);
var a003 = (document.contents.a3.value*1);
var a004 = (document.contents.a4.value*1);
var a005 = (document.contents.a5.value*1);
var a006 = (document.contents.a6.value*1);
var a007 = (document.contents.a7.value*1);
var a008 = (document.contents.a8.value*1);
var a009 = (document.contents.a9.value*1);
var a010 = (document.contents.a10.value*1);
var a011 = (document.contents.a11.value*1);
var a012 = (document.contents.a12.value*1);
… and so on until 88!

//assigns the current value of the ‘total’ box for each row to a new variable
var t001 = (document.contents.t1.value*1);
var t002 = (document.contents.t2.value*1);
var t003 = (document.contents.t3.value*1);
var t004 = (document.contents.t4.value*1);
var t005 = (document.contents.t5.value*1);
var t006 = (document.contents.t6.value*1);
var t007 = (document.contents.t7.value*1);
var t008 = (document.contents.t8.value*1);
var t009 = (document.contents.t9.value*1);
var t010 = (document.contents.t10.value*1);
var t011 = (document.contents.t11.value*1);
… and so on until 88!

//adds the new entry value to the total of the row

document.contents.t1.value = (a001+=t001); // does the math
document.contents.a1.value = ”; //clears the entry box

document.contents.t2.value = (a002+=t002);
document.contents.a2.value = ”;

document.contents.t3.value = (a003+=t003);
document.contents.a3.value = ”;

document.contents.t4.value = (a004+=t004);
document.contents.a4.value = ”;

document.contents.t5.value = (a005+=t005);
document.contents.a5.value = ”;

document.contents.t6.value = (a006+=t006);
document.contents.a6.value = ”;

document.contents.t7.value = (a007+=t007);
document.contents.a7.value = ”;

document.contents.t8.value = (a008+=t008);
document.contents.a8.value = ”;

document.contents.t9.value = (a009+=t009);
document.contents.a9.value = ”;

document.contents.t10.value = (a0010+=t0010);
document.contents.a10.value = ”;

document.contents.t11.value = (a0011+=t0011);
document.contents.a11.value = ”;

document.contents.t12.value = (a0012+=t0012);
document.contents.a12.value = ”;
… and so on until 88!

}

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@A1ien51May 12.2005 — you could try this, cut down on all your work!

<i>
</i>var max = 88;
for(i=1;i&lt;=max;i++){
document.contents["t"+i].value = parseInt(document.contents["t"+i].value) + parseInt(document.contents["a"+i].value);
document.contents["a"+i].value = ""
}


Eric
Copy linkTweet thisAlerts:
@mikencoauthorMay 12.2005 — This syntax is EXACTLY what I need!.. I had tried similar code, but couldn't quite it right!

Thank you thank you thank you! ?
×

Success!

Help @mikenco 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.5,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...