Help with math equation on my website


(Chris Potter) #1

I am looking for something I can use in Foundation and Stacks to do a basic math equation.

I just need a way for visitors to put in a number then have it automatically x 1.5% and gets the total

Any help or ideas would be great.

Thanks

Chris Potter


(Peter Danckwerts) #2

It should be easy with Javascript but I’m no good at Javascript!


(Will Woodgate) #3

Straightforward to do with vanilla Javascript. Here is a fiddle I setup for you and it can be tested:
https://jsfiddle.net/willwood/1fyyo6sv/

Here is the code in full:

<form method="post" action="">
	<input type="number" name="number" id="number" placeholder="0" />
	<input type="button" name="Add" value="Add 1.5%" onclick="willsCalc()" />
	<p id="answer"></p>
</form>

<script>
function willsCalc(){
	    var userinput = document.getElementById("number").value;
	    var percentage = Number(userinput) * (1.5/100);
	    var sum = Number(userinput) + Number(percentage);
	    document.getElementById("answer").innerHTML = "<strong>Answer:</strong> " + sum;
}
</script>

When the form is submitted on-click, the JS function runs (willsCalc). We get the user submitted number back from the form as a variable and then use a couple of other variables to make the calculation. The final step is to write the calculation back out into the page, to display to the sum.

So working on the assumption a user types 22 in the box and hits the submit button, their answer will be shown as 22.33.

22 + 1.5% = 22.33


(Chris Potter) #4

THANK YOU !!! Very cool , thanks for the help