Jump to content

[SOLVED] Help with functions


141n

Recommended Posts

Hey guys, firstly, hi - I'm a new member, and like all new members, I have a problem lol (easy on the jokes)

 

I'm trying to use JavaScript to do a simple calculation taken from data a user enters in a normal textbox. the Calculation is this: ($userdata * 0.25) + 0.10;

 

So I wrote a function to work it out and return the answer. The function works fine, but the problem is I just don't know how to get it to read the data that has been input by the user?

 

Here's what I've got so far:

 

<script type="text/javascript">

function calcCharge(Length,Rate,Connection) {

 

document.write("£",(Length*Rate) + Connection);

 

}

</script>

 

<input type="button" id="calcButton" value="Calculate Charge" onclick="calcCharge(20,0.25,0.10)">

 

how could I change the first argument passed to the calcCharge function to something like the ID of a text field? When I do that, it just doesn't work?

Link to comment
Share on other sites

You could possibly change the function to:

 

<script type="text/javascript">

function calcCharge(node, Length, Rate, Connection) {

    node.value= "£",(Length*Rate) + Connection;

}

</script>

 

<input type="button" id="calcButton" value="Calculate Charge" onclick="calcCharge(this, 20, 0.25, 0.10)">

 

OR

 

<script type="text/javascript">

function calcCharge(Length, Rate, Connection) {

    document.getElementById("calcButton").value = "£" + ((Length*Rate) + Connection);

}

</script>

 

<input type="button" id="calcButton" value="Calculate Charge" onclick="calcCharge(20, 0.25, 0.10)">

 

EDIT:

Fixed some typos.

Link to comment
Share on other sites

Sorry, I feel a bit silly for not explaining myself too well. what I wanted was for the user to enter a figure into a text box ($user_num) and then for the data to be passed to the function like so:

 

function calcCharge (Length, Rate, Connection) {

// do math

}

<input type="text" id="<b>user_num</b>" onclick(calcCharge(<b>user_num</b>, 0.20, 0.10)" />

 

Here's what I've got so far:

 

<script type="text/javascript">

function calcCharge(Length,Rate,Connection) {

 

var num = (Length*Rate) + Connection;

var totalCharge = num.toFixed(2);

 

document.getElementById("showCharge").innerHTML+='£' + totalCharge + '';

 

}

</script>

 

<input type="text" id="<b>user_num</b>" />

Link to comment
Share on other sites

OH OK, I misunderstood.

 

Here's what you do.

 

Change the textfield to:

 

   <input type="text" id="user_num" onclick="calcCharge(this, 0.20, 0.10)" />

 

and the function to this:

 

<script type="text/javascript">
function calcCharge(Node, Rate, Connection) 
{

    var Length        = Node.value;
    var num           = (Length*Rate) + Connection;
    var totalCharge   = num.toFixed(2);

    document.getElementById("showCharge").innerHTML+='£' + totalCharge + '';

}
</script>

 

There you go, that should do it!

 

***

Here's another way to do the same thing (just for reference--but the first option is the better of the two)

 

<input type="text" id="user_num" onclick="calcCharge('user_num', 0.20, 0.10)" />

<script type="text/javascript">
function calcCharge(id, Rate, Connection) 
{

    var Length        = document.getElementById(id).value;
    var num           = (Length*Rate) + Connection;
    var totalCharge   = num.toFixed(2);

    document.getElementById("showCharge").innerHTML+='£' + totalCharge + '';

}
</script>

Link to comment
Share on other sites

Oh, then you may want to try doing some type casting, i forgot it'll treat textfield values as strings:

 

<script type="text/javascript">
function calcCharge(Node, Rate, Connection) 
{

    var Length        = Number(Node.value);
    var num           = (Length*Rate) + Connection;
    var totalCharge   = num.toFixed(2);

    document.getElementById("showCharge").innerHTML+='£' + totalCharge + '';

}
</script>

Link to comment
Share on other sites

hehe the second method you listed that uses the (id) method works fine for me - it was that which I was going for to start with. Thanks for your help mate; I'm needing to study up some more on the ol' JS, at least learn more advanced stuff, and how to do this kind of stuff aswell

 

Thanks again, the final thing I'm using is this:-

 

<script type="text/javascript">

function calcCharge(id, Rate, Connection) {

 

    var Length        = document.getElementById(id).value;

    var num          = (Length*Rate) + Connection;

    var totalCharge  = num.toFixed(2);

 

    document.getElementById("showCharge").innerHTML+='£' + totalCharge + '';

}

function clearCharge() {

    document.getElementById("showCharge").innerHTML=' ';

}

</script>

 

<input type="text" id="user_num" />

<input type="button" value="Calculate Charge" onclick="calcCharge('user_num', 0.25, 0.10)" />

<input type="button" value="Refresh" onclick="clearCharge()" />

<div id="showCharge">

Amount Of Charge:

</div>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.