Solved

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

Posted on 2007-12-03
5
4,555 Views
Last Modified: 2013-12-07
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
Comment
Question by:ishwarjoshi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 29

Accepted Solution

by:
Göran Andersson earned 500 total points
ID: 20398360
Change

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

to

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

Author Comment

by:ishwarjoshi
ID: 20401519
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
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 20401618
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
 

Author Comment

by:ishwarjoshi
ID: 20407482
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
 
LVL 29

Expert Comment

by:Göran Andersson
ID: 20407971
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I recently found myself in a Corporate Situation where the client had requested blocking access to any and all websites except his own Domain? Easy? I am sure this would be your answer but their requirement was, this has to be done without using…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
This Micro Tutorial will demonstrate how nuggets on the Web are formatted by using Chrome Developer Tools. These tools would not only view the site's CSS but it can also modify it and save the CSS to use on your own site.
Shows how to create a shortcut to site-search Experts Exchange using Google in the Chrome browser. This eliminates the need to type out site:experts-exchange.com whenever you want to search the site. Launch the Search Engine Menu: In chrome, via you…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question