/    Sign up×
Community /Pin to ProfileBookmark

Addition Calculation

Simple maths calculation.

I have 3 field on a page. Field A having x values choosen from a drop down menu. Field B having y values choosen from a drop down menu. As soon as a person chooses a value from A or B fields the same should appear in text box ‘C’. And if values are choosen from both A and B then a total of A and B should show in C.

<p>A <select size=”1″ name=”A”>
<option value=”60″>60 xxxxxxx</option>
<option value=”90″>90 xxxxxx</option>
</select></p>
<p>B <select size=”1″ name=”B”>
<option value=”500″>yyyyyy -500</option>
<option value=”8000″>yyyyy – 8000</option>
</select></p>
<p>C&nbsp; <input type=”text” name=”C” size=”20″></p>

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@Willy_DuittAug 12.2004 — Try this:

<i>
</i>&lt;form&gt;
&lt;p&gt;A &lt;select size="1" name="A"
[color=red]onchange="this.form.C.value=this.value*1+this.form.B.value*1"[/color]&gt;
&lt;option value="0"&gt;Choose xxxx&lt;/option&gt;
&lt;option value="60"&gt;60 xxxxxxx&lt;/option&gt;
&lt;option value="90"&gt;90 xxxxxx&lt;/option&gt;
&lt;/select&gt;&lt;/p&gt;
&lt;p&gt;B &lt;select size="1" name="B"
[color=red]onchange="this.form.C.value=this.value*1+this.form.A.value*1"[/color]&gt;
&lt;option value="0"&gt;Choose yyyy&lt;/option&gt;
&lt;option value="500"&gt;yyyyyy -500&lt;/option&gt;
&lt;option value="8000"&gt;yyyyy - 8000&lt;/option&gt;
&lt;/select&gt;&lt;/p&gt;
&lt;p&gt;C &lt;input type="text" name="C" size="20"&gt;&lt;/p&gt;
&lt;/form&gt;


.....Willy
Copy linkTweet thisAlerts:
@CharlesAug 12.2004 — [font=monospace]<form>

<fieldset>

<label>A <select size="1" name="A"

onchange="this.form.C.value = Number (this.value) + Number (this.form.B.value)">

<option value="0">Choose xxxx</option>

<option value="60">60 xxxxxxx</option>

<option value="90">90 xxxxxx</option>

</select></label>

<label>B <select size="1" name="B"

onchange="this.form.C.value = Number (this.value) + Number (this.form.A.value)">

<option value="0">Choose yyyy</option>

<option value="500">yyyyyy -500</option>

<option value="8000">yyyyy - 8000</option>

</select></label>

<label>C <input type="text" name="C" size="20"></label>

</fieldset>

</form>[/font]
Copy linkTweet thisAlerts:
@Willy_DuittAug 12.2004 — Personally, I find using the [b]number()[/b] method a waste of time in such an instance when multiplying by one converts the string to a number in a much more efficient manner...

Unless of course your point was to add fieldsets and labels, but then again, that was not the question...

.....Willy
Copy linkTweet thisAlerts:
@GurusGuruauthorAug 12.2004 — Thanks
Copy linkTweet thisAlerts:
@CharlesAug 12.2004 — [i]Originally posted by Willy Duitt [/i]

[B]Personally, I find using the [b]number()[/b] method a waste of time in such an instance when multiplying by one converts the string to a number in a much more efficient manner...



Unless of course your point was to add fieldsets and labels, but then again, that was not the question...



.....Willy [/B]
[/QUOTE]
Actually, multiplying by one is less efficient. For each value String.valueOf() is called, then a multiplication is performed and then the desired addition. Casting each value as a Number (not a number, they're two different things) calls String.valueOf() for each and then the desired addition. That's one fewer steps on each side of the additive operator. If we really want to cut out steps then we could use [font=monospace]this.form.C.value = this.value.valueOf() + this.form.B.value.valueOf()"[/font]

But I was only trying to demonstrate what might be a more intuitive way to get JavaScript to treat a string as a number.

And yes, I did make some changes to the mark up in your example. It is important to explicitly associate each form element with its label. And since the FORM element can only directly contain block level elements and since the LABEL element, like the form control elements, is an inline level element I added the block level element FIELDSET.
Copy linkTweet thisAlerts:
@Willy_DuittAug 12.2004 — My example was intended to show the O/P how to work the javascript into his example, not demonstrate how his example should be coded which chances are would be ignored and only the relative javascript used anyway...

And I still believe multiplying by one is more effecient than invoking another method ([i]twice[/i]) as well as I find it more intuitive. But then again, I routinely use this shortcut to convert strings to numbers...

Cheers;

.....Willy
×

Success!

Help @GurusGuru 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,
)...