Jump to content

Recommended Posts

Posted

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> 


Posted

Error is: "document.getElementById("amaranthcups") is null"

 

 

Because:

 

Amaranth flour: <input type="text" id="amaranthflour" name="amaranthflour" />

 

It's not amaranthcups it's amaranthflour

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...