Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-15-2012, 11:36 AM   PM User | #1
Reveal
New to the CF scene

 
Join Date: Jul 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Reveal is an unknown quantity at this point
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
Reveal is offline   Reply With Quote
Old 07-15-2012, 11:50 AM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,043
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
<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
__________________

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.

Last edited by Philip M; 07-15-2012 at 11:57 AM..
Philip M is offline   Reply With Quote
Old 07-15-2012, 11:53 AM   PM User | #3
Reveal
New to the CF scene

 
Join Date: Jul 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Reveal is an unknown quantity at this point
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
Reveal is offline   Reply With Quote
Old 07-15-2012, 11:59 AM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,043
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
__________________

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.

Last edited by Philip M; 07-15-2012 at 12:02 PM..
Philip M is offline   Reply With Quote
Old 07-15-2012, 12:02 PM   PM User | #5
Reveal
New to the CF scene

 
Join Date: Jul 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Reveal is an unknown quantity at this point
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
Reveal is offline   Reply With Quote
Old 07-15-2012, 12:05 PM   PM User | #6
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,043
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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;
}
__________________

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.

Last edited by Philip M; 07-15-2012 at 12:52 PM..
Philip M is offline   Reply With Quote
Old 07-15-2012, 12:07 PM   PM User | #7
Reveal
New to the CF scene

 
Join Date: Jul 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Reveal is an unknown quantity at this point
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:
Reveal is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:48 PM.


Advertisement
Log in to turn off these ads.