I have created the foloowing javascript and placed it in a file called common.js. it gets called on a onChange event. it works up till it gets to setting the value (i think). Being my first script i'm really at a loss why it errors out. I tried .value() = and .value( prevbal - demand..... TIA

function recalcnet(txt) {
			switch (txt) {
				case '0' :
				    recalcnet1();
				    break;}}
		function recalcnet1() {
				var prevbal = 0;
				var demand = 0;
				var whatif = 0;
				var sched = 0;
				prevbal = document.getElementById('BegInvTextbox').value;
				demand = document.getElementById('dm1TextBox').value;
				whatif = document.getElementById('ifm1TextBox').value;
				sched = document.getElementById('fmm1TextBox').value;
		        document.getElementById('nm1TextBox').value(prevbal - demand + whatif + sched)};

Here is little example to get you started on debugging. I would place some alerts in there to see what's going on. I am not exactly sure what you’re trying to do with the line below.

You already know the value of prevbal… etc. If you want to set the element below to the value of those numbers try something like I did below. I am guessing those var are numbers. But in your code they will be treated as strings.

document.getElementById('nm1TextBox').value(prevbal - demand + whatif + sched)
<html><head>
<title></title>
<script>
function testFn()
{	
	var test1 = document.getElementById('Num1').value;
	if(test1 != "")
	{
		alert("Number 1: " + test1);
	}
	
	var test2 = document.getElementById('Num2').value;
	if(test2 != "")
	{
		alert("Number 2: " + test2);
	}
	
	if (test1 != "" && test2 != "")
	{
		var test3 = document.getElementById('Total');
		var total = test3.value = parseInt(test1) + parseInt(test2);
		alert("Total Num 1 + Num2: " + total);
	}
}
</script>
</head>

<body>
<label value"num1"> Number 1 </label> <input size="2" id="Num1" onChange="testFn()">
<label value"num2"> + Number 2 </label> <input size="2" id="Num2"  onChange="testFn()">

<label value="fullName"> Total: </label> <input size="4" id="Total" type=text>


</body>
</html>

Because JavaScript isn't strongly-typed... and the default type of textboxes and labels is a string.

This article has been dead for over six months. Start a new discussion instead.