catandmouse
catandmouse

Reputation: 11809

Dynamic dropdown options not showing in IE7

We have a dropdown list that is dynamically populated using javascript (below). But it doesn't show in IE7.

This is the code that populates the options:

<script language="javascript" type="text/javascript">
        window.onload = function() {
        var today= new Date();
        var year= today.getFullYear();
        var val =0;
        t2= 51;
        grad_yr                 = document.getElementById("grad_yr");
        grad_yr.options[0]  = new Option("Year",val,false,false);
        var y=year;
        var yy=y;       
        for (var i=1; i <t2 ; i++) {
            grad_yr.options[i] = new Option(y,y);
            y=yy-1;
            yy--;
        }
        }
</script>

This is the HTML:

<select name="grad_yr" id="grad_yr"></select>

What could be wrong?


EDIT: Ok, nevermind. Apparently, the list is actually being populated. It's just that we have another javascript that sort of moves the position of the options that's why it looks hidden. Thanks anyway!

Upvotes: 1

Views: 1180

Answers (2)

KP Taylor
KP Taylor

Reputation: 2100

You've created a name collision by using the same global variable name for your grad_yr variable and the select element. Comment out the line where you are pulling your grad_yr from the document by ID, as it is unneccessary:

//grad_yr = document.getElementById("grad_yr");
grad_yr.options[0] = new Option("Year", val, false, false);

Or, if you really do need to have a variable reference, just give your variable a different name, like this (or some such):

grad_yrVar = document.getElementById("grad_yr");
grad_yrVar.options[0] = new Option("Year", val, false, false);
...
 for (var i = 1; i < t2; i++) {
     grad_yrVar.options[i] = new Option(y, y);
     y = yy - 1;
     yy--;
 }

...and it should work.

Upvotes: 1

Anuraj
Anuraj

Reputation: 19598

Try this

var grad_yr = document.getElementById("grad_yr");

Upvotes: 0

Related Questions