141n Posted August 12, 2007 Share Posted August 12, 2007 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? Quote Link to comment Share on other sites More sharing options...
Guest Posted August 12, 2007 Share Posted August 12, 2007 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. Quote Link to comment Share on other sites More sharing options...
141n Posted August 12, 2007 Author Share Posted August 12, 2007 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>" /> Quote Link to comment Share on other sites More sharing options...
Guest Posted August 12, 2007 Share Posted August 12, 2007 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> Quote Link to comment Share on other sites More sharing options...
141n Posted August 12, 2007 Author Share Posted August 12, 2007 hmmm, comes back sayin £NaN - Not A Number? I'll try it again, I may have screwed up some of your code =S Quote Link to comment Share on other sites More sharing options...
Guest Posted August 12, 2007 Share Posted August 12, 2007 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> Quote Link to comment Share on other sites More sharing options...
Guest Posted August 12, 2007 Share Posted August 12, 2007 Actually I just tried it out, with or without typecasting, it works for me. But use the typecast anyway, just in case. Quote Link to comment Share on other sites More sharing options...
141n Posted August 12, 2007 Author Share Posted August 12, 2007 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> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.