Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4562
  • Last Modified:

JavaScript code to loop through Dropdown list options not working in FireFox (works in IE)

This code works in IE but not in FF. Please help:


window.onload = blah;
 
function blah()
{
 
..
..
 
 
var which = document.getElementById('TheList');
for(i=0;i<which.length;i++)
{
  if(which.options[i].value == '" & Session("PreSelValue") & "')
   {
    which.selectedIndex = i;
    window.document.Form2.elements['TheTextBox'].value = which.options[i].value; 
    break;
   }
}
 
..
..
}

Open in new window

0
ishwarjoshi
Asked:
ishwarjoshi
  • 3
  • 2
1 Solution
 
Göran AnderssonCommented:
Change

for(i=0;i<which.length;i++)

to

for(i=0;i<which.options.length;i++)
0
 
ishwarjoshiAuthor Commented:
Thanks for the suggestion. That didn't work though. I put this in:

            response.write("for(i=0;i<which.options.length;i++)")
            response.write("{")
            response.write("alert(which.options[i].value);")

And it shows only a single blank alert. But the drop down list has 6 items.

This is an asp page. The drop down list in question is data-bound. This JavaScript code runs in the window.onload.

Do you think that the data bound list is not populated when this code runs? Maybe IE populates the list before the script runs and FireFox does not populate the list? That's my guess?

Please advise.

Thanks.
0
 
Göran AnderssonCommented:
Ok, then there is something more that is wrong...

Have you checked what the generated code looks like? Does the list contain the items? Does the list have exactly the id that you use in the code? Do you have any other element with the same id?

> Maybe IE populates the list before the script runs and FireFox does not populate the list?

Not at all. The data binding is done on the server, so the list contains the items before it's even sent to the browser. The browser has absolutely nothing to do with the data binding.
0
 
ishwarjoshiAuthor Commented:
Thanks for your answer.
Hmmm. Yes I checked the generated code. The list does contain the items and it also has the same Id. Basically it all works perfectly in IE so not sure what's missing in FF.
Any other ideas?
0
 
Göran AnderssonCommented:
Usually when something like that happens, there is some error in the page, but it doesn't necessary have to be directly related to the code where you notice the problem. Firefox is a bit stricter about the correctness of the code, while IE accepts certain mistakes in the code. Difference in case of id:s/names or duplicate id:s are such things that may make a difference.

Other than what I mentioned before, I don't see anything wrong with the code that you have shown.

If you open the Error Console in Firefox and browse to your page, do you get any error messages?
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now