PDA

View Full Version : Simple Calculations



TrollSlayer
9th November 2007, 11:54 PM
I'm wanting to know how I could develop a webpage containing javascript and three text boxes. I want to be able to input a beginning number, an ending number, and then the script calculate the percentage of change.

FirePenguins
12th November 2007, 09:37 PM
Sorry I don't really have time to actually write it for you (darn school!!) but this should help: http://www.w3schools.com/js/js_functions.asp

That's a tutorial about functions and lots of other javascript tutorials plus html tutorials on that site. Basically make an html form with two input boxes and one button. With the button use javascript to do an onclick function that calculates the percent change of the numbers. Hope this helps!

TrollSlayer
16th November 2007, 08:12 PM
<HTML>
<HEAD>
<TITLE></TITLE>
<SCRIPT language="JavaScript" type="text/javascript">
<!-- Hide Script from old browsers

function calc () {


}

// End hiding script from old browsers -->
</SCRIPT>


</HEAD>
<BODY>

Old Value:<input type="text" name="old">
<br><br>
New Value:<input type="text" name="new">
<br>
<input type="submit" oncllick="calc ()">

</BODY>
</HTML>

I'm uncertain on how to tell the function to take the new value and divide it by the old value. Can anyone show me how to do that?

Shane-
18th November 2007, 06:14 AM
<script type="text/javascript">
function calc()
{

var old = document.calcform.old.value;
alert("Old = " + old);
}
</script>

<FORM name="calcform">
Old Value:<input type="text" name="old" size="20">
<br><br>
New Value:<input type="text" name="new" size="20">
<br>
<button onclick="calc()">Click me</button>
</FORM>Shane

TrollSlayer
19th November 2007, 01:38 AM
Your script is only returning the old value in the popup. I need to divide the new value by the old value.

Shane-
19th November 2007, 06:17 AM
The script shows you how to get the values out of the input boxes, I had thort you'd wanted to do the rest as doing things yourself helps you to learn better

Anywhoh:


<script type="text/javascript">
function calc()
{
var firstvalue = document.calcform.first.value;
var secondvalue = document.calcform.second.value;
var newvalue = firstvalue / secondvalue;
alert(secondvalue + " is " + newvalue + " percent of " + firstvalue);
}
</script>

<FORM name="calcform">
Old Value:<input type="text" name="first" size="20">
<br><br>
New Value:<input type="text" name="second" size="20">
<br>
<button onclick="calc()">Click me</button>
</FORM>

Shane

TrollSlayer
19th November 2007, 09:33 AM
Right now I'm at the stage where I am examining codes that work and trying to piece together why they work...and was this code you just gave me supposed to be fully functional? If so, there is no alert popping up.

FirePenguins
21st November 2007, 09:01 PM
I copied and pasted code and it worked fine for me in firefox.

Shane-
23rd November 2007, 05:30 AM
Code worked fine for me in both IE+FF, Make sure you have javascript enabled on the browser your using, Hopefully FF :p

Edit: You did copy/paste the code without editing it?, If i rember right, You cant use the var name 'new' in js, AKA " var new = document.calcform.new.value; " wouldnt work, Which is why i used firstvalue/secondvalue/newvalue as var names

If you have Microsoft Frontpage installed, Copy/Paste the code into a .html page, And hit 'Preview', If theres any errors, frontpage will give you the line number of the line that the problem is with

Shane