/    Sign up×
Community /Pin to ProfileBookmark

Sample Code Calculation Form Multiplying 4 Numbers

Hello everyone I have a long time to program and I would apreciate if you could help me.

I need to write a simple html form.

My form looks like this:

integer variable a11=1
integer variable a12=2

(1) Textbox User Input Integer
(2) Textbox User Input Integer
(3) Textbox fixed non editable value 15%
(4) Dropdown Yes/No

If (4) = Yes then Total = (1)*a11
If (4) = No then Total = (2)*
a12

Thanks

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@MouryaSep 15.2011 — 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>
[/CODE]
Copy linkTweet thisAlerts:
@feaxauthorSep 17.2011 — Thanks a lot Mourya. Now I have a starting point to build my form.
×

Success!

Help @feax spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...