You don't get the result because you never assign a value to the element
txtPerimeter
. Also, when you try to get an element to be used to output perimeter value, you override the numeric value previously calculated for this variable. As JavaScript is, apparently, a scripting language, which uses
loose type system, the same variable first gets the numeric value equal to the perimeter value, but later on, you override this value with the DOM element object representing the control with the
id="txtPerimeter"
.
That should be enough to resolve the problem (if other parts are correct). It should be something like
elementPerimeter = document.getElementById('txtPerimeter');
elementPerimeter.value = txtWidth * 2 + txtLength * 2;
You should better avoid those confusing prefixes "txt" in variable names. And, at least at first, try not to re-use the variables. If some variable represents the numeric value, use it for numeric values only; assign it first and modify as calculations go, or not. If some variable represents the DOM element, calculate it once (like using
getElementById
) and, in most simple cases, never modify.
Please see:
http://en.wikipedia.org/wiki/Loose_typing[
^],
http://en.wikipedia.org/wiki/JavaScript[
^].
It looks like you need to learn a lot of very basic stuff. JavaScript is pretty difficult for understanding and debugging, honestly. You might need to master some very basic general programming techniques on something much more simple (at least when it comes to basics), such as C#.
—SA