0

So I have a javascript to add together values entered, that works just fine. I need to add the value of the selected radio button to this equation. I was adding by Id value, so I imagine sticking to that would be great. I cannot figure this out...I've searched but everywhere just indicates how to validate the selection.

I don't think this is duplicated...I need the value of the selected radio button and the value has to be added to the equation.

Radio Options
<input type="radio" name="int" id="0" value="none">None
<input type="radio" name="int" id="10" value="Ten">10
<input type="radio" name="int" id="20" value="Twenty">20
<input type="radio" name="int" id="30" value="Thirty">30

This is what my javascript looks like:

function calculate()

{
var userInput = document.getElementById("pkg").value*1;
var userInput2 = document.getElementById("pep").value*1;
var addBox = document.getElementById("addtl").value*1;
var totalNum = document.getElementById("add_cost").value*1;

 // Sum everything
var SumAll = userInput + userInput2 + (addBox*totalNum);

// print the total 
document.getElementById("Sum").innerHTML = SumAll.toFixed(2)

}
VashTS
  • 27
  • 2
  • 11

1 Answers1

0

Just check which of the radio buttons is "checked", like this:

var values = document.getElementsByName("int");

for(var i = 0; i < values.length; i++) {
   if(values[i].checked == true) {
       selectedValue = values[i].value;
   }

}

The one that is selected will have checked == true.

See this Fiddle for an example (use the console to see the buttons being selected).

  • and this will be found in the link posted by BloodyKnuckles http://stackoverflow.com/questions/15839169/how-to-get-value-of-selected-radio-button#answer-15839283 – NewToJS Mar 13 '15 at 01:00
  • How does this create the variable? This checks it but doesn't give me the variable to add to the equation. – VashTS Mar 13 '15 at 01:28
  • I've tried adding selectedValue, values but nothing works. not even a NaN just no computation. Dang this is difficult!! – VashTS Mar 13 '15 at 02:18