Jump to content

[SOLVED] getElement() returning a different value


gathos

Recommended Posts

ok so i have an estimate form that figures out how much a peice of glass costs per sq inch.

but one of the variables are being multiplied by 10

 

thi sis my code:

<script type="text/javascript">
				var cprice = <? echo $_GET["cprice"]; ?>;
				function estimate()
				{

					var height = document.getElementById('glassheight').value;
					var height16 = document.getElementById('glassheight16').value;
					var totalheight = height + height16;

					var width = document.getElementById('glasswidth').value;
					var width16 = document.getElementById('glasswidth16').value;
					var totalwidth = width + width16;

					var ctotal = totalwidth * totalheight * cprice;
					var sqfttotal = ctotal / 144;

					document.write(height);
					document.write("<br>");
					document.write(width);
					document.write("<br>");
					document.write(totalwidth);
					document.write("<br>");
					document.write(totalheight);
					document.write("<br>");
					document.write(sqfttotal);
					document.write("<br>");
					document.write(ctotal);
					document.write("<br>");
					document.write(cprice);


					if (sqfttotal <= (cprice * 2))
					{
						document.getElementById('cprice').value = (cprice * 2);
					}
					else
					{

						document.getElementById('cprice').value = MATH.round(sqfttotal*100)/100;
					};	
				};
                 </script>
                 Width:  
                 <input type="text" name="glasswidth" id="glasswidth" /> 
                 <select name='glasswidth16' id='glasswidth16'>
                  <option value='0'>Choose sixteenth</option>
                  <option value='0'>0</option>
                  <option value='0.0625'>1/16</option>
                  <option value='0.125'>1/8</option>
                  <option value='0.1875'>3/16</option>
                  <option value='0.250'>1/4</option>
                  <option value='0.3125'>5/16</option>
                  <option value='0.375'>3/8</option>
                  <option value='0.4375'>7/16</option>
                  <option value='0.500'>1/2</option>
                  <option value='0.5625'>9/16</option>
                  <option value='0.625'>5/8</option>
                  <option value='0.6875'>11/16</option>
                  <option value='0.750'>3/4</option>
                  <option value='0.8125'>13/16</option>
                  <option value='0.875'>7/8</option>
                  <option value='0.9375'>15/16</option>
                </select>
                 <br />
                 X<br />
Height: 
<input type="text" name="glassheight" id="glassheight" />
                 <select name='glassheight16' id='glassheight16'>
                  <option value='0'>Choose sixteenth</option>
                  <option value='0'>0</option>
                  <option value='0.0625'>1/16</option>
                  <option value='0.125'>1/8</option>
                  <option value='0.1875'>3/16</option>
                  <option value='0.250'>1/4</option>
                  <option value='0.3125'>5/16</option>
                  <option value='0.375'>3/8</option>
                  <option value='0.4375'>7/16</option>
                  <option value='0.500'>1/2</option>
                  <option value='0.5625'>9/16</option>
                  <option value='0.625'>5/8</option>
                  <option value='0.6875'>11/16</option>
                  <option value='0.750'>3/4</option>
                  <option value='0.8125'>13/16</option>
                  <option value='0.875'>7/8</option>
                  <option value='0.9375'>15/16</option>
                </select>
                 <br />
                 <br />
                 <input type="button" onClick="estimate()" value="Estimate" />
                 
                 
                 
                 </p>
                   <br>
                   <br>
                 All Clear Plain Glass
                     <input type="text" name="cprice" id="cprice" /></td>
                     
                 
                     
                 

 

i haven't even got to the if statement yet, because when i test out the variables (the document.write section) totalheight and totalwidth are being times by 10.  IE: i put in 30 and 300 is displayed for those 2. please help, i need a second pair of eyes to help me find my bug.

 

thanks ahead of time,

gathos

I didn't check the math, but I can fix your javascript errors:

var totalheight = height + height16;

It is concatenating because height and height16 are both strings.

 

Typecast them to integers by putting a + in front

var totalheight = +height + +height16;

OR

Parse them as ints

var height = parseInt(document.getElementById('glassheight').value);

 

The same is for width.

thank you, converting to an integer did help, i realized being a string it was litteraly adding the 0 from my other input select's.

 

and also i figured out that there was another problem, with my math.round() I had to put it onto a seperate line first, then i was able to output it.

 

thanks again and later days,

gathos

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.