www.webdeveloper.com
Results 1 to 3 of 3

Thread: getElementBy Id does not work in IE

  1. #1
    Join Date
    Jun 2009
    Posts
    1

    getElementBy Id does not work in IE

    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
    }


    </script>
    -----------------
    HTML
    <p>Honors Distribution Requirements:<br />
    Group-I: 6 or more credits in courses offered in these departments:<br /> </p>
    <table width="90%" border="1" cellpadding="6" onmouseover= "showtotg1credits()">
    <tr>
    <th width="22%">Course Number</th>
    <th width="45%">Course Title</th>
    <th width="33%">Credits</th>
    <th width="33%"><p>APCredits<br />Bypassed</p></th>
    <tr>
    <td><input name="g1cn1" type="text" id="g1cn1" maxlength="9" /></td>
    <td><label>
    <input name="g1c1" type="text" id="g1c1" size="50" maxlength="50" />
    </label></td>
    <td><select name="g1credits1" size="1" id="g1credits1">
    <option>select</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    </select></td>
    <td><label>
    <input type="checkbox" name="g1apb" id="g1apb1" />
    </label></td>
    </tr>
    <tr>
    <td><input type="text" name="g1cn2" id="g1cn2" /></td>
    <td><input name="g1c2" type="text" id="g1c2" size="50" maxlength="50" /></td>
    <td><select name="g1credits2" size="1" id="g1credits2">
    <option>select</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    </select></td>
    <td><input type="checkbox" name="g1apb2" id="g1apb2" /></td>
    </tr>
    <tr>
    <td><input type="text" name="g1cn3" id="g1cn3" /></td>
    <td><input name="g1c3" type="text" id="g1c3" size="50" maxlength="50" /></td>
    <td><select name="g1credits3" size="1" id="g1credits3">
    <option>select</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    </select></td>
    <td><input type="checkbox" name="g1apb3" id="g1apb3" /></td>
    </tr>
    <tr>
    <td><input type="text" name="g1cn4" id="g1cn4" /></td>
    <td><input name="g1c4" type="text" id="g1c4" size="50" maxlength="50" /></td>
    <td><select name="g1credits4" size="1" id="g1credits4">
    <option>select</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    </select></td>
    <td><input type="checkbox" name="g1apb4" id="g1apb4" /></td>
    </tr>
    <tr>
    <td><input type="text" name="g1cn5" id="g1cn5" /></td>
    <td><input name="g1c5" type="text" id="g1c5" size="50" maxlength="50" /></td>
    <td><select name="g1credits5" size="1" id="g1credits5">
    <option>select</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    </select></td>
    <td><input type="checkbox" name="g1apb5" id="g1apb5" /></td>
    </tr>
    <tr>
    <td>Total Group I Credits
    <input type="text" name="tg1credits" id="tg1credits" />
    </td>

    </tr>
    </table>

  2. #2
    Join Date
    Oct 2008
    Location
    U.S.
    Posts
    726
    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;
    }

  3. #3
    Join Date
    Oct 2008
    Location
    U.S.
    Posts
    726
    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;
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles