dcsimg
www.webdeveloper.com
Results 1 to 4 of 4

Thread: Form Field Additiion

  1. #1
    Join Date
    Mar 2013
    Posts
    16

    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>

  2. #2
    Join Date
    Dec 2005
    Location
    TX
    Posts
    7,916
    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.

  3. #3
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,433
    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 document.getElementById('ead2').options[document.forms['form1'].elements['ead2'].selectedIndex].getAttribute('id'); 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.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  4. #4
    Join Date
    Mar 2013
    Posts
    16
    Thanks all for your help. I experimented with JMRKER's idea and figured it out.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center