/    Sign up×
Community /Pin to ProfileBookmark

Shorten code *newbie*

I’m still learning this scripting thing. Is there any way to shorten this lengthy code. I know there is, just not sure what i’m looking for?

PA1_10ST
PA11_20ST
PA21_30ST
PA31_40ST
PA41_50ST
PA51_60ST
PA61_70ST
PA71_80ST
PA81_90ST
PA91_100ST

This has over 100 dropdown’s

[CODE]
<script type=”text/javascript”>
function update()
{

document.getElementById(“PA1_10ST”).value =
parseFloat(document.getElementById(“PA1”).value)
+
parseFloat(document.getElementById(“PA2”).value)
+
parseFloat(document.getElementById(“PA3”).value)
+
parseFloat(document.getElementById(“PA4”).value)
+
parseFloat(document.getElementById(“PA5”).value)
+
parseFloat(document.getElementById(“PA6”).value)
+
parseFloat(document.getElementById(“PA7”).value)
+
parseFloat(document.getElementById(“PA8”).value)
+
parseFloat(document.getElementById(“PA9”).value)
+
parseFloat(document.getElementById(“PA10”).value);

document.getElementById(“PC1_10ST”).value =
parseFloat(document.getElementById(“PC1”).value)
+
parseFloat(document.getElementById(“PC2”).value)
+
parseFloat(document.getElementById(“PC3”).value)
+
parseFloat(document.getElementById(“PC4”).value)
+
parseFloat(document.getElementById(“PC5”).value)
+
parseFloat(document.getElementById(“PC6”).value)
+
parseFloat(document.getElementById(“PC7”).value)
+
parseFloat(document.getElementById(“PC8”).value)
+
parseFloat(document.getElementById(“PC9”).value)
+
parseFloat(document.getElementById(“PC10”).value);

document.getElementById(“PA51_60ST”).value =
parseFloat(document.getElementById(“PA51”).value)
+
parseFloat(document.getElementById(“PA52”).value)
+
parseFloat(document.getElementById(“PA53”).value)
+
parseFloat(document.getElementById(“PA54”).value)
+
parseFloat(document.getElementById(“PA55”).value)
+
parseFloat(document.getElementById(“PA56”).value)
+
parseFloat(document.getElementById(“PA57”).value)
+
parseFloat(document.getElementById(“PA58”).value)
+
parseFloat(document.getElementById(“PA59”).value)
+
parseFloat(document.getElementById(“PA60”).value);

document.getElementById(“PC51_60ST”).value =
parseFloat(document.getElementById(“PC51”).value)
+
parseFloat(document.getElementById(“PC52”).value)
+
parseFloat(document.getElementById(“PC53”).value)
+
parseFloat(document.getElementById(“PC54”).value)
+
parseFloat(document.getElementById(“PC55”).value)
+
parseFloat(document.getElementById(“PC56”).value)
+
parseFloat(document.getElementById(“PC57”).value)
+
parseFloat(document.getElementById(“PC58”).value)
+
parseFloat(document.getElementById(“PC59”).value)
+
parseFloat(document.getElementById(“PC60”).value);

document.getElementById(“PremTotalAdded”).value = parseFloat(document.getElementById(“PA1_10ST”).value) + parseFloat(document.getElementById(“PA51_60ST”).value);
document.getElementById(“PremTotalCancelled”).value = parseFloat(document.getElementById(“PC1_10ST”).value) + parseFloat(document.getElementById(“PC51_60ST”).value);

document.getElementById(“Netpercent”).value = parseFloat(document.getElementById(“PA1_10ST”).value) – parseFloat(document.getElementById(“PC1_10ST”).value) / “20”;

}
</script>

[/CODE]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERFeb 12.2011 — Try this:
<i>
</i>&lt;script type="text/javascript"&gt;
function $_(IDS) { return document.getElementById(IDS); }

function update() {
var sum;
sum = 0; for (var i=1; i&lt;=10; i++) { sum += parseFloat($_('PA'+i).value); }
$_("PA1_10ST").value = sum;
sum = 0; for (var i=1; i&lt;=10; i++) { sum += parseFloat($_('PC'+i).value); }
$_("PC1_10ST").value = sum;
sum = 0; for (var i=1; i&lt;=9; i++) { sum += parseFloat($_('PA5'+i).value); }
$_("PA51_60ST").value = sum + parseFloat($_("PA60").value);
sum = 0; for (var i=1; i&lt;=9; i++) { sum += parseFloat($_('PC5'+i).value); }
$_("PC51_60ST").value = sum + parseFloat($_("PC60").value);

$_("PremTotalAdded").value = parseFloat($_("PA1_10ST").value) + parseFloat($_("PA51_60ST").value);
$_("PremTotalCancelled").value = parseFloat($_("PC1_10ST").value) + parseFloat($_("PC51_60ST").value);
$_("Netpercent").value = parseFloat($_("PA1_10ST").value) - parseFloat($_("PC1_10ST").value) / "20";
}
&lt;/script&gt;

Untested, but should be close.

Good Luck!

?
Copy linkTweet thisAlerts:
@DJScriptauthorFeb 13.2011 — I tired the code and didn't work. i'll give you the full list of what i got.

Tried to post it and more than 10000 lines ugh!!!

i'll have to post then provide like
Copy linkTweet thisAlerts:
@JMRKERFeb 13.2011 — I tired the code and didn't work. ...[/QUOTE]

That kind of description is not very helpful.

It's like calling the hospital, saying your sick and asking what needs to be fixed!

What did not work?

What kind of errors did you get.

Where did the errors occur.

What browser are you using?

Are you using an error console?
Copy linkTweet thisAlerts:
@DJScriptauthorFeb 21.2011 — The error i was getting:

Webpage error details

User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0;

Message: Object required

Line: 16

Char: 41

Code: 0

Message: Object required

Line: 16

Char: 41

Code: 0


Tired

google, ie firefox, safari, opera


I'll post the full code.

http://starportal.ath.cx/code.txt
×

Success!

Help @DJScript 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.7,
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,
)...