Hi...I wrote a javascript which works perfectly well in firefox and chrome, but not in IE...It is a simple script which adds the credithours(drop down box) selected by the user..and displays them on the screen...
The intent behind the script is to add all the credits selected by the student, and display them, but in IE it doesnt work.Any help is really appreciated
<script type = "text/javascript">
function totcredits(){
var array = new Array()
var j
for(j=1;j<=5;j++)
{
array[j-1] = parseInt(document.getElementById('g1credits'+j).value)
}
var sum = 0
for(i=0;i<array.length;i++)
{
if(isNaN(array[i]))
{
array[i]=0
}
sum = sum + array[i]
}
return sum
}
function showtotg1credits()
{
var xxx = totcredits()
var ans = document.getElementById('tg1credits')
ans.value=xxx
}
Change your totcredits function to the following (it is best, and most cross browser compatible to access select option values in the long notation - )
Code:
function totcredits() {
var i, j, el, val, array = [], sum = 0;
for (j = 1; j <= 5; j++) {
el = document.getElementById('g1credits'+j);
val = el.options[el.options.selectedIndex].value;
array[j-1] = parseInt(val,10);
}
for (i=0;i<array.length;i++) {
if (isNaN(array[i])) {
array[i]=0;
}
sum = sum + array[i];
}
return sum;
}
Although, actually there's really no need for the second loop, or the array in there, could just make the totcredits like this:
Code:
function totcredits() {
var j, el, val, num, sum = 0;
for (j = 1; j <= 5; j++) {
el = document.getElementById('g1credits'+j);
val = el.options[el.options.selectedIndex].value;
num = parseInt(val,10);
sum += (isNaN(num)) ? 0 : num;
}
return sum;
}
Bookmarks