Here is the sample code,
you haven't mentioned what is the use of percentage textbox,
also if you to have just Yes or No as choice , its better to use radio button.
Here we are calling the calculate() function whenever the button is clicked
then access the elements in the page, and do the calculations.
Code:
<head>
<title>Simple Calculator</title>
<script type="text/javascript">
var a1=1,a2=2;
function calculate()
{
var choice=document.getElementById('choice');
choice=choice.options[choice.selectedIndex].text;
var txt1=document.getElementById('txt1').value;
var txt2=document.getElementById('txt2').value;
var txtper=document.getElementById('txtper').value;
var txtresult=document.getElementById('txtresult');
choice=="Yes"?txtresult.value=txt1*a1:txtresult.value=txt2*a2;
}
</script>
</head>
<body>
First Input :<input type="text" id="txt1" /><br />
Second Input:<input type="text" id="txt2" /><br />
Percentage:<input type="text" id="txtper" value="15" readonly="readonly" /><br />
Choice<select id="choice">
<option value="Yes">Yes</option>
<option value="No">No</option>
</select>
<br />
Result:<input type="text" id="txtresult" /><br />
<input type="button" value="Calculate" onclick="calculate()" />
</body>
</html>
Bookmarks