Javascript: How to get the value from a checked radio button when more than one radio button is present

user2108839

I am trying to get the value from a checked radio button and eventually pass that value into other functions. Currently I am only successful when I have one radio button in my html. As soon as I add another radio button with the same name my Javascript function does not work as I intended.

Here is my Javascript:

function RadioButtonValue (form) {
if (form.elements ["radio"].checked) {
return parseInt (form.elements["radio"].value)
}else{
return 0}
}

function totalRadio (form) {
var radio = RadioButtonValue (form)
form.elements ["total"].value =  radio
}

Here is my HTML:

        <form>
            <input type="radio" name="radio" value="1" />1
            <br />
            <input type="radio" name="radio" value="2"/>2
            <br />
            <input type="button" value="Radio Button Value" onclick="javascript:totalRadio(this.form)" />
            <br />
            <input type="number" name="total" />
    </form>

I am looking for the simplest Javascript solution, I think using a "for loop" is the solution I need but I am not sure.

Thanks for the help in advance!

atxpunkrock

Here's a loop that will just iterate through the radio buttons in the form that's being processed so it doesn't add any other checked radio buttons.

function RadioButtonValue (form) {    
   var returnVal = 0;
   var radios = form.elements ["radio"];
   for(var r = 0; r < radios.length; r++) {
        if (radios[r].checked) { 
            returnVal = parseInt (radios[r].value);
        }
   }
  return returnVal;
}

function totalRadio (form) {
    var radio = RadioButtonValue (form)
    form.elements ["total"].value =  radio
}

Here it is in a fiddle: http://jsfiddle.net/PM8Jm/1/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to get checked radio button value in javascript

From Dev

How to get the text from radio button in a radio group when radio button checked state is changed

From Dev

How to get the value of the checked radio button and the remaining td's but not include the radio button that has not been checked (in tr)

From Dev

how to get only one radio button value in javascript

From Dev

How can I get the text of radio button if radio button checked?

From Dev

how get radio button value in javascript

From Dev

Can't get value of checked radio button

From Dev

javascript radio button return previous checked value

From Dev

How can i keep the radio button checked and get the value

From Dev

Jquery how to get checked radio button when clicked an list item?

From Dev

Get text node from checked radio button

From Dev

How to retrieve radio button value from database in checked state in jsp

From Dev

How do I get which radio button is checked from a groupbox?

From Dev

How do I get which radio button is checked from a groupbox?

From Dev

How to display checked value for radio button collection

From Dev

How to display the checked radio button value in angularJs

From Dev

How to get checked radio button in list view?

From Dev

How to get radio button checked in AngularJS?

From Dev

Javascript radio button deselect others when selecting one radio button?

From Dev

If radio button checked submit value?

From Dev

get the checked radio button in each radio group

From Dev

Issue with Radio button checked in JavaScript

From Dev

Get radio button value

From Dev

How to check radio button with radio button value

From Dev

C# VS - When one radio button is checked, uncheck another

From Dev

Getting value of checked radio button by name of radio button?

From Dev

Get radio button value on submit with JavaScript

From Dev

Get selected radio button value in javascript

From Dev

How to get the NAME and value of a radio button when DYNAMICALLY selected - in jQuery?

Related Related

  1. 1

    how to get checked radio button value in javascript

  2. 2

    How to get the text from radio button in a radio group when radio button checked state is changed

  3. 3

    How to get the value of the checked radio button and the remaining td's but not include the radio button that has not been checked (in tr)

  4. 4

    how to get only one radio button value in javascript

  5. 5

    How can I get the text of radio button if radio button checked?

  6. 6

    how get radio button value in javascript

  7. 7

    Can't get value of checked radio button

  8. 8

    javascript radio button return previous checked value

  9. 9

    How can i keep the radio button checked and get the value

  10. 10

    Jquery how to get checked radio button when clicked an list item?

  11. 11

    Get text node from checked radio button

  12. 12

    How to retrieve radio button value from database in checked state in jsp

  13. 13

    How do I get which radio button is checked from a groupbox?

  14. 14

    How do I get which radio button is checked from a groupbox?

  15. 15

    How to display checked value for radio button collection

  16. 16

    How to display the checked radio button value in angularJs

  17. 17

    How to get checked radio button in list view?

  18. 18

    How to get radio button checked in AngularJS?

  19. 19

    Javascript radio button deselect others when selecting one radio button?

  20. 20

    If radio button checked submit value?

  21. 21

    get the checked radio button in each radio group

  22. 22

    Issue with Radio button checked in JavaScript

  23. 23

    Get radio button value

  24. 24

    How to check radio button with radio button value

  25. 25

    C# VS - When one radio button is checked, uncheck another

  26. 26

    Getting value of checked radio button by name of radio button?

  27. 27

    Get radio button value on submit with JavaScript

  28. 28

    Get selected radio button value in javascript

  29. 29

    How to get the NAME and value of a radio button when DYNAMICALLY selected - in jQuery?

HotTag

Archive