Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Percentage Calculator HELP

    So here is my code so far:
    Code:
    <SCRIPT language=JavaScript>
    function perc1() {
     a = document.form1.a.value/100;
     b = a*document.form1.b.value;
     document.form1.total1.value = b
     }
    function perc2() {
     a = document.form1.c.value;
     b = document.form1.d.value;
     c = a/b;
     d = c*100;
     document.form1.total2.value = d
     }
    </SCRIPT>
    Code:
    <div><span style="font-size: 24px; font-weight: bold; text-decoration: underline;"><br />
        </span>
      <form name="form1">
        <table cellspacing="1" cellpadding="1" border="0">
          <tbody>
            <tr>
              <td>Exchanges under $100 = 
                <input type="hidden" size="5" value="25" name="a" />25% of 
                <input size="5" name="b" />? </td>
              <td>Answer: 
                <input maxlength="40" size="5" name="total1" disabled="disabled" /></td>
              <td>
                <input onclick="perc1()" type="button" value="Calculate" /></td>
            </tr>
          </tbody>
        </table>
      </form></div>
    This basically works out 25% of the input, now how I do make a new tr tag that will auto substract the input(b) - the percentage answer (total1) and display the value

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,149
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    <input maxlength="40" size="5" name="total1" disabled="disabled" /></td>

    Why disabled? Do you perhaps intend readonly?

    a = document.form1.a.value/100;

    Do not use the same name for an HTML form element and a Javascript variable. Some browsers will get confused. It is very poor practice to assign meaningless names to elements and variables which give no clue as to what they are supposed to be. And what is the pont of having this value in a hidden field? And its value is 25 (a number), not "25" (a string). That can be fatal if a + sign is involved.

    You have omitted the var keyword so all the variables are global and those in perc1() will be over-written by those of the same name in perc2().

    <script language=javascript> is long deprecated. Use <script type = "text/javascript"> instead (in fact also deprecated but still necessary for IE<9).

    Just make a new table cell and write the result of your calculation to it. What is your problem here?

    It is your responsibility to die() if necessary….. - PHP Manual
    Last edited by Philip M; 07-15-2012 at 11:57 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    <input maxlength="40" size="5" name="total1" disabled="disabled" /></td>

    Why disabled? Do you perhaps intend readonly?

    a = document.form1.a.value/100;

    Do not use the same name for an HTML form element and a Javascript variable. Some browsers will get confused.

    You have omitted the var keyword so all the variables are global and those in perc1() will be over-written by those of the same name in perc2().

    <script language=javascript> is long deprecated. Use <script type = "text/javascript"> instead (in fact also deprecated but still necessary for IE<9).

    Just make a new table cell and write the result of your calculation to it. What is your problem here?

    It is your responsibility to die() if necessary….. - PHP Manual
    Yes the field is read only

    Still doesn't answer my questions:
    How do I:input(b) - the percentage answer (total1) and display the value

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,149
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Quote Originally Posted by Reveal View Post
    Yes the field is read only

    Still doesn't answer my questions:
    How do I:input(b) - the percentage answer (total1) and display the value
    One more time, just make a new table cell and write the result of your calculation to it. What is your problem here?

    But it appears that b and total1 are the same value:-

    b = a*document.form1.b.value;
    document.form1.total1.value = b


    As this is clearly homework you write the code and then someone will doubtless correct/improve it for you. But first you must make the corrections to the errors that I have pointed out.
    Last edited by Philip M; 07-15-2012 at 12:02 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    One more time, just make a new table cell and write the result of your calculation to it. What is your problem here?

    As this is clearly homework you write the code and then someone will doubtless correct/improve it for you.
    Oh great forum, not.

    it is a form that finds out exchange rates, for example someone wanting to exchange 100 Payza to "Liberty Reserve" the fee will be 25% which is 25

    So I want to create a code/tr that will show the result of the field of Value Entered - Percentage Total1 which I don't know how to do

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,149
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Quote Originally Posted by Reveal View Post
    Oh great forum, not.
    Well, that ends any further help from me. Have a look at Forum Rule 1.5 about homework assignments.

    Code:
    var rude = true;
    var thanks = false;
    if (rude && !thanks) {
    var interestLost = true;
    var moreHelp = 0;
    }
    Last edited by Philip M; 07-15-2012 at 12:52 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #7
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    Well, that ends any further help from me. Have a look at Forum Rule 1.5 about homework assignments.
    No thanks, enjoy downtime on forum D:


  •  

    Posting Permissions

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