/    Sign up×
Community /Pin to ProfileBookmark

Trying to make a ‘calculator’

Hi,

This is what I’m trying to do:
I have 2 textboxes, and I want the value (number) entered in the first textbox to be used in an equation…

e.g. if I put any number in the first textbox, it will be multiplied by 12, and the answer will be shown in the second textbox…

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@HiyaOct 02.2005 — [code=php]
<html>
<head>

<style type="text/css">
<!--

//-->
</style>

<script language="javascript" type="text/javascript">
<!--

function doCal(){
document.f.txt2.value = document.f.txt1.value * 12;
}

//-->
</script>

</head>


<body>

<form name="f">

<input type="text" name="txt1"><br>
<input type="text" name="txt2"><br>
<input type="button" value="Calculate!" onclick="doCal();">

</form>

</body>
</html>
[/code]


[upl-file uuid=4d132671-4df4-4037-9033-788095efddc8 size=334B]Calculator.zip[/upl-file]
Copy linkTweet thisAlerts:
@asianinvasionauthorOct 03.2005 — OK, so how could I make it so that if the "1" radio box is checked, it will be:

document.stormpay.fees.value = document.stormpay.recieve.value * 0.01 + 0.30;

and if the "2" radio box is checked:

document.stormpay.fees.value = document.stormpay.recieve.value * 0.02 + 0.30;

and "3"

document.stormpay.fees.value = document.stormpay.recieve.value * 0.03 + 0.30;

&lt;html&gt;
&lt;head&gt;

&lt;style type="text/css"&gt;
&lt;!--

//--&gt;
&lt;/style&gt;

&lt;script language="javascript" type="text/javascript"&gt;
&lt;!--

function doCal(){
document.stormpay.fees.value = document.stormpay.recieve.value * xxxxxx + 0.30;
document.stormpay.total.value = document.stormpay.recieve.value - document.stormpay.fees.value;
}

//--&gt;
&lt;/script&gt;

&lt;/head&gt;


&lt;body&gt;

&lt;table border="1"&gt;
&lt;tr&gt;
&lt;td&gt;

&lt;form name="stormpay"&gt;

&lt;INPUT TYPE=RADIO NAME="1" CHECKED&gt;0.01x&lt;BR&gt;
&lt;INPUT TYPE=RADIO NAME="2"&gt;0.02x&lt;BR&gt;
&lt;INPUT TYPE=RADIO NAME="3"&gt;0.03x&lt;P&gt;

<i> </i>&lt;/td&gt;
<i> </i>&lt;td&gt;

Money recieves: &lt;input type="text" name="recieve"&gt;&lt;br&gt;
Fees: &lt;input type="text" name="fees"&gt;&lt;br&gt;
Net: &lt;input type="text" name="total"&gt;&lt;br&gt;
&lt;input type="button" value="Calculate!" onclick="doCal();"&gt;

<i> </i>&lt;/td&gt;
<i> </i>&lt;/tr&gt;
&lt;/table&gt;

&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;


Also, how can I specify how many decimal places to show?

And also prevent it from going to a negative number (negative number = 0)
Copy linkTweet thisAlerts:
@asianinvasionauthorOct 03.2005 — ...anyone?
Copy linkTweet thisAlerts:
@NedalsOct 03.2005 — 
  • 1. A group of 'radio buttons' should all have the same name

  • 2. Note the method used to set 'rate'. This saves a loop within the function.

  • 3. 'receive' was spelled wrong!
    <i>
    </i>&lt;html&gt;
    &lt;head&gt;

    &lt;script type="text/javascript"&gt;
    &lt;!--
    var rate = "0.01"; //default.
    function doCal() {
    var df = document.forms[0];
    if (df.receive.value &gt; 0) {
    df.fees.value = ((df.receive.value * rate) + 0.30).toFixed(2);
    df.total.value = (df.receive.value - df.fees.value).toFixed(2);
    } else {
    alert("Money received must greater than $0");
    }
    }
    //--&gt;
    &lt;/script&gt;
    &lt;/head&gt;

    &lt;body&gt;
    &lt;form name="stormpay"&gt;
    &lt;p&gt;
    &lt;INPUT TYPE=RADIO NAME="r" CHECKED onclick="rate=0.01"&gt;0.01x&lt;BR&gt;
    &lt;INPUT TYPE=RADIO NAME="r" onclick="rate=0.02"&gt;0.02x&lt;BR&gt;
    &lt;INPUT TYPE=RADIO NAME="r" onclick="rate=0.03"&gt;0.03x
    &lt;/p&gt;
    &lt;p&gt;
    Money receives: &lt;input type="text" name="receive"&gt;&lt;br&gt;
    Fees: &lt;input type="text" name="fees"&gt;&lt;br&gt;
    Net: &lt;input type="text" name="total"&gt;&lt;br&gt;
    &lt;input type="button" value="Calculate!" onclick="doCal();"&gt;
    &lt;/p&gt;
    &lt;/form&gt;

    &lt;/body&gt;
    &lt;/html&gt;
  • ×

    Success!

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