Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
date format angularjs2 16 40
Javascript in a user control not working 17 52
using web browser with BING 40 86
Create a new template 9 21
Citrix XenApp, Internet Explorer 11 set to Enterprise Mode and using central hosted sites.xml file.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This Micro Tutorial will demonstrate how to add subdomains to your content reports. This can be very importing in having a site with multiple subdomains.
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 +…

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now