Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3322910
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:14:21+00:00 2026-05-17T23:14:21+00:00

I must admit, I don’t know much about JavaScript that is why my question

  • 0

I must admit, I don’t know much about JavaScript that is why my question might sound little bit silly.

But what I’m trying to do is grab values from selected by name radio groups.

It looks like this

function calc() {
    var op1 = document.getElementsByName('form[radio1]');
    var op2 = document.getElementsByName('form[radio2]');
    var op3 = document.getElementsByName('form[radio3]');

    var result = document.getElementById('result');

    result.value = 0;

    result.value = parseInt(result.value);

    for (i = 0; i < op1.length; i++) {
        if (op1[i].checked) result.value = parseInt(result.value) + parseInt(op1[i].value);
    }

    for (i = 0; i < op2.length; i++) {
        if (op2.options[i].selected) result.value = parseInt(result.value) + parseInt(op2[i].value);
    }

    for (i = 0; i < op3.length; i++) {
        if (op3.options[i].selected) result.value = parseInt(result.value) + parseInt(op3[i].value);
    }

    return false;
}

And this is my form. Im using rs form for joomla.

<form action="index.php" enctype="multipart/form-data" id="userForm" method="post">

    <input name="form[radio1]" value="25" id="radio20" type="radio">
        <label for="radio20">Description1</label>

    <input name="form[radio1]" value="35" id="radio21" type="radio">
        <label for="radio21">Description2</label>



    <input name="form[radio2]" value="20" id="radio20" type="radio">
    <label for="radio20">Description1</label>

    <input name="form[radio2]" value="30" id="radio21" type="radio">
    <label for="radio21">Description2</label>


    <input type="hidden" value="0" id="result" name="form[result]">

    <input type="submit" class="rsform-submit-button" onclick="calc()" id="submit" name="form[submit]" value="submit">

And everything would be OK, as the function is working. the only trouble is that I have about 80 radiograms.

Is there a way to shorten it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T23:14:22+00:00Added an answer on May 17, 2026 at 11:14 pm

    Use arrays of objects (like all the radio buttons, for instance) and iterate over them. Start like this:

    var opts = [],
        numOpts = 80;
    
    for (var i=0; i<numOpts, i++)
    {
        opts.push(document.getElementsByName('form[radio' + i + ']'));
    }
    

    Edit: let’s have a go at the full function. The only thing I’m not 100% sure about is whether you mean to use opX[i].checked or opX.options[i].selected (since your code does different things for op1 and op2/3). Shouldn’t be too hard to extrapolate if I’ve guessed wrong, though.

    function calc()
    {
        var opts = [],
            numOpts = 80,
            value = 0,
            result = document.getElementById('result'),
            i, j, opt;
    
        for (i=0; i<numOpts; i++)
        {
            opts.push(document.getElementsByName('form[radio' + i + ']'));
        }
    
        numOpts = opts.length;
    
        for (i=0; i<numOpts; i++)
        {
            opt = opts[i];
            for (j=0; j<opt.length; j++)
            {
                // or did you mean:
                // if (opt.options[j].selected) ?
                if (opt[j].checked)
                {
                    value = value + parseInt(opt[j].value, 10);
                }
            }
        }
    
        result.value = value;
    
        return false;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Accepting the possibility of extreme ridicule, I must admit that I really miss sun
This must be a classic .NET question for anyone migrating from Java. .NET does
There must be a generic way to transform some hierachical XML such as: <element1
I must implement a web service which expose a list of values (integers, custom
I must be getting daft, but I can't seem to find how to read
I must confess I'm somewhat of an OOP skeptic. Bad pedagogical and laboral experiences
I must be overlooking something simple. I'm setting a variable from a query result
There must be an easy way to do this, but somehow I can wrap
I must be dense. After asking several questions on StackOverflow, I am still at
Why must class members declared as const be initialized in the constructor initializer list

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.