Hi there everyone. I have a problem with the JavaScript code for my aggregates workings for my course. I have worked out with some help how to input a number, click a button and get the number to appear in a listbox. I also had to get the Reset button to clear all fields including the listbox which I managed to work out.
I am now having problems getting it to total all the numbers in the list box. I have managed to get it to do something but get 'undefined' in the Total Sum box. I also need to average it but haven't really done much with sorting it out yet as it doesn't work either.
Would someone be so kind as to tell me where I am going wrong with this as I am still learning JavaScript
Below is the code that I have so far
<script>
function testAdd()
{
var formObject = document.aggregate
if (formObject.input.value!="")
addOption(formObject.List,formObject.input.value)
}
function addOption(selectObject,input)
{
var optionObject = new Option(input)
var optionRank = selectObject.options.length
selectObject.options[optionRank]=optionObject
}
function GetTotal()
{
var list = eval(document.getElementById('List').value)
document.getElementById('Total Sum').value = list ;
}
function average()
{
document.getElementById("Average").value = select.Avg("List");
}
function ListBoxClear()
{
document.getElementById('List').options.length = 0;
}
</script>
</head>
<body>
<h1>Aggregates</h1>
</p>
<strong> Add as many numbers as you like<br />
to the list, then click Calculate.</strong><br />
<form id="aggregate" name="aggregate" action="">
<label>
<input name="input" type="text" id="input" size="10" maxlength="10" />
</label>
<label>
<input id="Add" input type="button" value="Add to list" onclick="testAdd()"/>
</label>
<br />
<label>
<select name="List" multiple="MULTIPLE" id="List" size="10">
</select>
</label>
<br />
<input type="button" name="Calculate" id="Calculate" value="Calculate" onclick="GetTotal();average()" />
<label>
<input name="Reset" type="reset" id="Reset" onclick="ListBoxClear()" value="Reset" />
</label>
<p><strong>Total (Sum):</strong></p>
<p>
<label>
<input name="Total Sum" type="text" id="Total Sum" />
</label>
</p>
<p><strong>Average:</strong></p>
<p>
<label>
<input name="Average" type="text" id="Average" />
</label>
<br />
</p>
<label></label>
</form>