To answer @alex in more detail, you selected the onLoad jsFiddle option, which places your JavaScript code inside an anonymous function:
window.onload = function() { function energyCalc() { var mass = document.forms['0'].mass.value; var finalMass = mass * 1000; var velocity = document.forms['0'].velocity.value; var finalVelocity = velocity * 0.3048; var energy = (0.5 * (finalMass * (finalVelocity * finalVelocity))); if ((mass && velocity) != '') { document.getElementById("energy").innerHTML = 'Energy of weapon: ' + energy + ' Joules'; } else { document.getElementById("energy").innerHTML = 'You must enter values in both fields!'; } } }
Try this for size: http://jsfiddle.net/mattball/Cbsa8/ . All I did was select no wrap (head) and No library .
Keep in mind that it is better to use encoding to write unobtrusive JavaScript . In this case, you are binding the event handler using JavaScript using attachEvent (for IE <9) or addEventListener (for real browsers).
Edit re: file editing
Open the console and the problem will immediately become apparent after trying to use the energy calculator:
> Uncaught TypeError: Cannot read property 'value' of undefined (fiddle.jshell.net:100)
which is this line:
var mass = document.forms['0'].mass.value;
You get access to the wrong form. You need to change all cases of forms['0'] to forms[1] in the code of the energy calculator. Pay attention to the missing quotes, by the way - document.forms is an array, not an object!
Also, you are explicitly using jQuery, but you are not using it wherever you can. Why not? There are many opportunities to improve your code. This is a cleaning time!
HTML
<div id="main"> <a href="#"><h3>Battery Charge Calculator</h3></a> <div class="slide"> <form id="batteryForm" action=""> <p> Battery Capacity (mah): </p> <p> <input name="capacity"/> </p> <p> Charge Rate (mA): </p> <p> <input name="rate"/> </p> <p> <input type="submit" value="Submit"/> </p> <br/> <p id="time"/> </form> </div> <a href="#"><h3>Energy Calculator</h3></a> <div class="slide"> <form id="energyForm" action=""> <p> Mass of BB (grammes): </p> <p> <input name="mass"/> </p> <p> Power of weapon (FPS): </p> <p> <input name="velocity"/> </p> <p> <input type="submit" value="Submit"/> </p> <br/> <p id="energy"/> </form> </div> </div>
Javascript
$(function() { $('#main > a > h3').click(function() { $(this).closest('a').next('div.slide').animate({ height: 'toggle' }, 750); }); function batteryCalc() { var capacity = this.capacity.value, rate = this.rate.value, time = capacity / (rate / 1.2), chargeTime = (Math.round(10 * time) / 10).toFixed(1), message = 'You must enter values in both fields!'; if (capacity && rate) { message = 'Required time on charge: ' + chargeTime + ' hours'; } $('#time').html(message); return false; } function energyCalc() { var mass = this.mass.value, finalMass = mass * 1000, velocity = this.velocity.value, finalVelocity = velocity * 0.3048, energy = (0.5 * (finalMass * (finalVelocity * finalVelocity))).toFixed(1), message = 'You must enter values in both fields!'; if (mass && velocity) { message = 'Energy of weapon: ' + energy + ' Joules'; } $('#energy').html(message); return false; } $('#batteryForm').submit(batteryCalc); $('#energyForm').submit(energyCalc); });