JasonDavis Posted March 11, 2011 Report Posted March 11, 2011 Hi all, I'm a complete Javascript newbie trying to write a basic script: http://www.wvconfidential.com/temp/ All I'm trying to do is convert different types of flour from cups to grams. As you can see my constants indicate the weight equivalent for 1 cup converted to grams. Any ideas what I'm doing wrong? Thanks! Code of index.html: <html> <head> <title>The Flour Converter</title> <script type="text/javascript"> function convert() { const ALMOND = 112; const AMARANTH = 120; var almondCups = parseFloat(document.getElementById("almondcups").value); var amaranthCups = parseFloat(document.getElementById("amaranthcups").value); if (isNaN(almondCups)) almondCups = 0; if (isNaN(amaranthCups)) amaranthCups = 0; var almondGrams = almondCups * ALMOND; var amaranthGrams = amaranthCups * AMARANTH; document.getElementById("almondgrams").value = almondGrams.toFixed(2); document.getElementById("amaranthgrams").value = amaranthGrams.toFixed(2); } </script> </head> <body> <form name="input"> Almond flour in cups: <input type="text" id="almondcups" name="almondcups" /> Equivalent in grams: <input type="text" id="almondgrams" name="almondgrams" /> <br> Amaranth flour: <input type="text" id="amaranthflour" name="amaranthflour" /> Equivalent in grams: <input type="text" id="amaranthgrams" name="amaranthgrams" /> <br> <input type="button" value="Calculate!" onclick="convert();" /> </form> </body> </html> Quote
BeeDev Posted March 11, 2011 Report Posted March 11, 2011 Error is: "document.getElementById("amaranthcups") is null" Because: Amaranth flour: <input type="text" id="amaranthflour" name="amaranthflour" /> It's not amaranthcups it's amaranthflour Quote
JasonDavis Posted March 11, 2011 Author Report Posted March 11, 2011 Brilliant! Thanks! Do you have a recommended debug tool to use, that told you the error so easily? Quote
falkencreative Posted March 11, 2011 Report Posted March 11, 2011 Brilliant! Thanks! Do you have a recommended debug tool to use, that told you the error so easily? I usually use Firebug, a Firefox plugin. It has a "script" tag that shows errors like this. Quote
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.
Note: Your post will require moderator approval before it will be visible.