/    Sign up×
Community /Pin to ProfileBookmark

Form Field Additiion

I have the code below that adds the values of form fields and gives total amount. I got the code online as I am new to Javascript and can’t write it myself yet. It works fine but I now need to add another form field value to the addition calculation. I have searched the internet and tried various methods to solve the problem to no avail. The form field is a text input field called “tip”. I would think it would take a javascript expert about 30 seconds to write the new field into the existing code, thus saving my sanity. Help will be greatly appreciated.

[CODE]
<script type=”text/javascript”>
function Calc(){
var person1 =document.getElementById(‘ead2’).options[document.forms[‘form1’]
.elements[‘ead2’].selectedIndex].getAttribute(‘id’);
var person2 = document.getElementById(‘ess2’).options[document.forms[‘form1’]
.elements[‘ess2’].selectedIndex].getAttribute(‘id’);
var result = (parseInt(person1) + parseInt(person2));

var totalPassenter = result;
var luggage = document.getElementById(‘elg’).value;
var canCarry = totalPassenter * 2;
if(totalPassenter != 0 && luggage > canCarry) {
var extraLuggage = luggage – canCarry;
var extraPay = extraLuggage * 5;
document.getElementById(‘elg2’).value = extraPay;
} else{
document.getElementById(‘elg2’).value = 0;
}
VField1=eval(document.form1.ead2.value)
VField2=eval(document.form1.ess2.value)
VField5=eval(document.form1.ech2.value)
VField7=eval(document.form1.elg2.value)
d=VField1+VField2+VField5+VField7
document.form1.fare.value = d;
}
</script>
[/CODE]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERNov 14.2017 — You need to add a new element in the form name 'tip'

Assuming it will look like <input type='text' id="tip" value="">

Then add the access code similar to the other elements

in the script portion of your post.

Post back with you HTML and CSS if you have problems.
Copy linkTweet thisAlerts:
@rootNov 14.2017 — As you have not posted the complete markup, I will assume that you have not got a proper web form based on the use of [B]document.getElementById('ead2').options[document.forms['form1'].elements['ead2'].selectedIndex].getAttribute('id');[/B] to get a select value that doesn't require half that code.

If you post your Markup (HTML) then it would be possible to guide you to a much simpler script and better markup.
Copy linkTweet thisAlerts:
@ronh100authorNov 14.2017 — Thanks all for your help. I experimented with JMRKER's idea and figured it out.
×

Success!

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