Get value of radio button

Hello,
I have this code:

       <form name="due">
       <input type="text" value="200" size=5 name="u">
       <input type="radio" name="raise" value="50">
       <input type="radio" name="raise" value="100">
       <input type="radio" name="raise" value="200"></form>

this is javascript:

        var leroy = document.due.raise;

        for(i=0; i<leroy.length; i++){        
        if(leroy[i].checked){
        document.due.u.value=leroy[i];
        }

the error is:

        [object HTMLInputElement]

Can i get the value of radio button? Thanks.

that is not an error. you assign the radio button (element) itself instead of its value. and that is what you get when you cast a radio button element to a string.

I don’t know, can you show me how to do?

how do you get the value of an input element in JS in general?

I try this code, for display the input:

   for(i=0; i<leroy.length; i++){        

   if(leroy[i].checked){
   alert(leroy[i].value);
   }

   }

and is OK. but how to insert in text field? follow my code please…

exactly the same way.

maybe…document.due.u.leroy[i].value; not work…sorry how do i do;

leroy is part of raise, not of u, but why do you try to make it that complicated? you can already access the value in your code, why not doing it again like that?

I use a simple id and innerHTML for display the value…so i try, by by…thanks.

innerHTML does not work on <input> elements, since they are empty.

I try this code, id is punti…cash is generic value like a 500

      for(i=0; i<leroy.length; i++){        

      if(leroy[i].checked){

      punti.innerHTML = cash + eval(leroy[i].value);
      }

      }

how do I sum the values? Why sum once? I want to add more than once…you have an answare…please!

somebody can help me to write a function for sum the value…

how do I sum the values? Why sum once? I want to add more than once…you have an answare…please!

You might find it easier to do the total first, and then when you have the full total to update the value.

The value is always going to be a string, so as a safety precaution you can make it a number instead.

var total = 0;
...
for ... {
    total += Number(leroy[i].value);
}
punti.innerHTML = cash + total;

Sorry…when i call the function js read var total = 0…so the valor can’t be increment…how can i do?

Can you put an example of the code up somewhere such as jsfiddle or codepen, so that we can investigate further details in to the situation?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.