Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

HTML document using JavaScript.?

I want 4 radio buttons, Addition, Subtraction,Multiplication and Division. Upon clicking any one of them and then Submit, a prompt box should appear where the 1st number is entered.Then after the OK button is pressed, another prompt box should appear.Upon clicking OK again,the result should appear. Now I know how to do the latter half of the program, but I haven't used radio buttons before. I will be grateful to anyone who helps.

Thanks,

Sachin

2 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    <html>

    <head>

    <script type="text/javascript">

    function doOp( b ) {

    // get the collection of the radio button

    // for the form

    var radios = b.parentNode.op;

    // how many radio buttons

    var last = radios.length;

    // initialize operation holder here to

    // keep in function scope

    var op;

    for (var i = 0; i < last; i++) {

    // find radio that is checked and capture

    // its operation

    if (radios[ i ].checked) {

    op = radios[ i ].value;

    // quit {for} block as soon as found

    break;

    }

    }

    // prompt for input values

    var n1 = prompt( 'enter your first number', 0 );

    var n2 = prompt( 'enter your second number', 0 );

    // verify that division by zero has not been

    // requested - if it has warn user and exit

    if ('/' == op && 0 == n2) {

    alert( 'division by zero is undefined' );

    return;

    }

    // compose expression to be evaluted

    var x = n1 + op + n2 ;

    // notify user of expression and answer

    alert( x + ' = ' + eval( x ) );

    }

    </script>

    </head>

    <body>

    <!--

    Give all radio buttons the same name attribute - this ties them

    together logically, so only one button can be checked at a time.

    Give each radio the value that is the arithmetic operation to be

    performed for that button.

    -->

    <form>

    <input type="radio" name="op" value="+" /> Add

    <input type="radio" name="op" value="-" /> Subtract

    <input type="radio" name="op" value="*" /> Multiply

    <input type="radio" name="op" value="/" /> Divide

    <input type="button" value="do chosen operation" onclick="doOp( this )" />

    </form>

    </body>

    </html>

  • ?
    Lv 4
    4 years ago

    No via fact Javascript would not have report io. It additionally lacks get admission to the the checklist source that it rather is resident in. You do have get admission to to particular components in the checklist as long as they are defined in the previous the Javascript is achieved.

Still have questions? Get your answers by asking now.