/    Sign up×
Community /Pin to ProfileBookmark

Can I obtain form values using a for loop?

I have a series of one line text buttons that have numbers in them and I want to sum up those numbers. The text input names are a1, a2, a3…… I was thinking of writing a javaScript function something like this:

<script type=”text/javascript”>
function add_values(length_of_list) {
var total=0.0;
for (var i=1; i<=length_of list; i++){
name=’a’+i;

total+= document.formname.name.value;

}

}
</script>

But…..this won’t work, at least the way I have it. The name in document.formname.[B]name[/B].value will not be accepted.

Can anyone tell me if what I’m trying to do is possible? Any help and/or suggestions will be greatly appreciated!

  • Mike
  • to post a comment
    JavaScript

    2 Comments(s)

    Copy linkTweet thisAlerts:
    @fredmvSep 05.2004 — You can make dynamic field references like this:document.forms['formName'].elements['a' + i].value;
    Copy linkTweet thisAlerts:
    @Mike_BurdickauthorSep 05.2004 — fredmv,

    Thanks for your quick response and time! That's just what I needed to know - works great.

    -Mike
    ×

    Success!

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