0

Hi everyone can someone please help me with the following problem.

// i have written the problem inside the javascript

See http://jsfiddle.net/7Cmwc/3/ for example.

 function calculate() 
//If radiobutton with ID box3 is checked do this mybox1*mybox2+5
{
        var myBox1 = document.getElementById('box1').value; 
        var myBox2 = document.getElementById('box2').value;
        var result = document.getElementById('result'); 
        var myResult = myBox1 * myBox2 + 5 ;
        result.value = myResult;
      }

//If radiobutton with ID box4 is checked do this mybox1*mybox2+10
{
        var myBox1 = document.getElementById('box1').value; 
        var myBox2 = document.getElementById('box2').value;
        var result = document.getElementById('result'); 
        var myResult = myBox1 * myBox2 + 10 ;
        result.value = myResult;
      }

//If radiobutton with ID box3 is checked do this mybox1*mybox2+15
{
        var myBox1 = document.getElementById('box1').value; 
        var myBox2 = document.getElementById('box2').value;
        var result = document.getElementById('result'); 
        var myResult = myBox1 * myBox2 + 15 ;
        result.value = myResult;
      }

And also i wonder what the difference is between jradiobutton and radiobutton?

Thanks in advance

Sam
  • 4,994
  • 4
  • 30
  • 37
olzonpon
  • 73
  • 9

2 Answers2

0

Like this...

function calculate() {
var test = document.getElementsByName("radioGroupName")

                var sizes = test.length;
                alert(sizes);

                for (i = 0; i < sizes; i++) {
                    if (test[i].checked == true) {
                       whichIsCheckedValue = test[i].value;
whichIsChecked = i;

                    }
                }


}

Once you have that, you can run your conditions. Of course, there is a better way, if you look at how this is done. You should have your conditions in the same place that it's identifying what was checked.

durbnpoisn
  • 4,666
  • 2
  • 16
  • 30
0

Not saying I would do it this say, but this is a simple example to show you how it's done, at the very basic level.

function calculate()  {

    var myBox1 = document.getElementById('box1').value,
        myBox2 = document.getElementById('box2').value,
        myBox3 = document.getElementById('box3').checked,
        myBox4 = document.getElementById('box4').checked,
        myBox5 = document.getElementById('box5').checked,
        result = document.getElementById('result'),
        myResult;

    if ( myBox3 ) {
      myResult = myBox1 * myBox2 + 5 ;
      result.value = myResult;
    }

    else if ( myBox4 ) {    
       myResult = myBox1 * myBox2 + 10 ;
       result.value = myResult;
    }

    else if ( myBox5 ) {
       myResult = myBox1 * myBox2 + 15 ;
       result.value = myResult;
    }
}
bencripps
  • 2,025
  • 3
  • 19
  • 27