Solved

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

Posted on 2007-12-03
5
4,552 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
  • 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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

I had to do a bit of research to find the answer to this question so I thought I'd share my results.  Due to our outdated mainframe systems, we need to downgrade IE9 to IE8 in order to stay compatible.  We also needed to downgrade Java.  In order to…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…
How to create a custom search shortcut to site-search Experts Exchange using Google in the Firefox browser. This eliminates the need to type out site:experts-exchange.com whenever you want to search the site. Launch your Bookmark Menu: Press 'Ctrl +…

809 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