xjsc16x
xjsc16x

Reputation: 278

javascript calculation not working

I am trying to make a simple calculator in js for my father's website.

I have a few texboxes with user inputs that should calc fine, but it refuses to work. I simply cannot figure out what is wrong with it! http://jsfiddle.net/xjsc16x/WgKmn/

function foundationcalc() {
var height = parseFloat(document.getElementById("height").value, 0);
var thickness = parseFloat(document.getElementById("thickness").value, 0);
thickness = thickness / 12;
var length = parseFloat(document.getElementById("length").value, 0);
var width = parseFloat(document.getElementById("width").value, 0);
var calc1 = height * thickness * length
calc1 = calc1 / 27;
var calc2 = height * thickness * width
calc2 = calc2 / 27;
var total = (2 * calc1) + (2 * calc2);
total = parseFloat(Math.round(total * 100) / 100).toFixed(2);
document.getElementById("output").value = total;
}

I originally had (height * thickness * length) / 27, but I changed it in the hopes that that might be the error. no dice. Does anyone know of a javascript debugger where you can see the values of variables live? Would be a huge help to me!

Upvotes: 0

Views: 443

Answers (2)

James
James

Reputation: 1

You can simply add

window.onload = foundationcalc();

This will run the script at window loading.

Upvotes: -1

Ry-
Ry-

Reputation: 224858

The problem is that jsFiddle wraps your code in an onload handler by default. Changing it to reside directly in <head> fixes the problem.

Upvotes: 4

Related Questions