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

  • SEARCH
  • Home
  • 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
  • 3 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

I must admit that I don't really know if this is the right place
I must admit I don't remember much about HEX and so on from school
I must admit that I am incredibly jealous of those developers who happen to
I must admit the HTML5 semantic markup confuses me. I'm talking about these tags
I have this simple script that I'm working on. I must admit, I'm totally
This problem is about concurrent high speed inserts. I must admit it is very
I have just installed Microsoft Robotics Studio 2008 R2, and I must admit that
I've not used Zombies before, and must admit I'm not that sure what it's
I must admit, that usually I haven't bothered switching between the Debug and Release
first i must admit that i'm not very familiar with sql server's recursive CTE's

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.