Fix body language

html form not working with javascript function?

I built this code myself its suppost to take a user input value and run the function and spit the number back out but every time I try and run it I get a error code from line 40 (IE6), saying that the object does not support that atribute, but I know it does. I numbered the lines at the comment boxes. function: <script language="JavaScript"> <!-- var cost = 0; var lvl = 2; function ministercalc() { var lvltoget = window.document.ministercalc.lvltoget.value; while (lvl < lvltoget) { window.document.write ("working... " + lvl); cost = cost + (1000*lvl); lvl = lvl + 1; } window.document.write ("Total cost is: " + cost); } // --> </script> form: <div id="body"> <form name="ministercalc" action=''> Minister level to obtain:<br /> (line 40)<input type="text" name="lvltoget" value="" /> <br /> <input type="button" value="Run" onClick="ministercalc();" /> </form> I just want to know why its not working and what I can do to fix it.

Public Comments

  1. for the var lvltoget, give it a value of document.form('mistercalc') instead of window.document.mistercalc . It can't find your form object. Perhaps, just use the getElementById and ref the value of the text box, rather than walking down the tree.
  2. the value property of a text box is valid, but i suspect lvltoget is not referencing the textbox properly with this code. my advise - first, use an ID attribute in place of or in addition to the name attribute for your textbox: <input ID="lvltoget"...> then, in your function, use the getElementById method to get your object. var lvltoget = document .getElementById("lvltoget") .value lastly i would use a for loop instead. for (var lvl = 2; lvl < lvltoget; lvl++) { ... cost += (lvl*1000) }
Powered by Yahoo! Answers