Code:
<html>
<head>
<script>
function math(frm){
var num1=frm.v1.value.replace(/\D/g,'');
var num2=frm.v2.value.replace(/\D/g,'');
if (!num1||!num2){
alert('please enter 2 numbers');
}
else {
var rads=frm.rad,mess='Please Select One Radio';
for (var z0=0;z0<rads.length;z0++){ // rads = an array of the radio buttons
if (rads[z0].checked){
if (z0==0){
mess="Sum of two numbers is: "+(num1*1+num2*1); // * converts to a number
}
if (z0==1){
mess="Result is: "+(num1-num2); // - converts to a number
}
if (z0==2){
mess="Result is: "+(num1*num2); // * converts to a number
}
if (z0==3){
mess="Result is: "+(num1/num2); // / converts to a number
}
}
}
alert(mess);
}
}
</script>
</head>
<body>
<form name="form">
Value1:
<input type="number" name="v1">
Value2:
<input type="number" name="v2">
<br>
Addition
<input type="radio" value="add" name="rad" id="add">
<br>
Subtraction
<input type="radio" value="sub" name="rad" id="sub">
<br>
Multiplication
<input type="radio" value="mul" name="rad" id="mul">
<br>
Division
<input type="radio" value="div" name="rad" id="div">
<br>
<input type="button" value="Calculate" onclick="math(this.form);">
<input type="reset" value="Clear">
</form>
</html>
Bookmarks