473,769 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Browser Bug: Select dropdown element's add() method doesn't work as expected (IE)

acoder
16,027 Recognized Expert Moderator MVP
Problem
The select object's add method doesn't append options to the end of the list.

Browser
Internet Explorer

Example
The Javascript code for appending an option element at the end of a select dropdown list:
Expand|Select|Wrap|Line Numbers
  1. var opt = document.createElement("option");
  2. opt.name = "optName";
  3. opt.value = "optValue";
  4. var selObj = document.getElementById("seltest");
  5. selObj.add(opt,null);
Solution
IE does not support the W3C add method, so we have to use a try/catch to use its proprietary method instead:
Expand|Select|Wrap|Line Numbers
  1. var opt = document.createElement("option");
  2. opt.name = "optName";
  3. opt.value = "optValue";
  4. var selObj = document.getElementById("seltest");
  5. try {
  6.  selObj.add(opt,null);
  7. } catch (e) {
  8.    selObj.add(opt); // for IE only
  9.   }
-----------------------------------------------------------------------------------------------------
Problem
The select object's add method doesn't add options to the list. This is similar to the previous problem (appending to the end of the list), but with a slightly different solution.

Browser
Internet Explorer

Example
The Javascript code for adding an option element to a select dropdown list:
Expand|Select|Wrap|Line Numbers
  1. var opt = document.createElement("option");
  2. opt.name = "optName";
  3. opt.value = "optValue";
  4. var selObj = document.getElementById("seltest");
  5. // let's say we want to add before the currently selected option
  6. var optToAddBefore = selObj.options[selObj.selectedIndex];
  7. selObj.add(opt,optToAddBefore);
Solution
IE does not support the W3C add method, so we have to use a try/catch to use its proprietary method instead. For the second argument, instead of the option element, we have to pass the selected index:
Expand|Select|Wrap|Line Numbers
  1. var opt = document.createElement("option");
  2. opt.name = "optName";
  3. opt.value = "optValue";
  4. var selObj = document.getElementById("seltest");
  5. // let's say we want to add before the currently selected option
  6. var optToAddBefore = selObj.options[selObj.selectedIndex];
  7. try {
  8.  selObj.add(opt,optToAddBefore);
  9. } catch (e) {
  10.    selObj.add(opt,selObj.selectedIndex); // for IE only
  11.   }
More Bugs, Quirks and Inconsistencies
Aug 18 '07 #1
0 7347

Sign in to post your reply or Sign up for a free account.

Similar topics

5
2974
by: Ganco | last post by:
We have a dropdown combobox, where some of the text is wider than the textarea. I cannot set the width of the SELECT any higher due to design restrictions on the company website. When clicking the dropdown box, I want the textarea (which unfolds beneath the dropdown) to expand to the widest element of the options. I can see that Netscape does this automatically, but how would you do it in IE?
13
9459
by: Kai Grossjohann | last post by:
It seems that Ctrl-N in Mozilla opens a new empty browser window. That's fine, I don't need to do anything about it. But Ctrl-N in IE appears to clone the current window. Is there a way to intercept the key so that I can do stuff on the server side to make the new window behave correctly? (We have a JSP-based webapp which stores state in the session. Now if two windows access (and modify!) the same session, then madness will result....
0
4071
by: Francis Parsons | last post by:
Hi! I'm doubtful there's a workaround for this problem, but it's worth a shot. I'm using IE's (css) "zoom" property to zoom the body of a document (in conjunction with persistance so the previously selected percentage level is restored each time the page is reloaded). I don't expect zoomed images to look perfect, but overall the quality of the zooming is pretty good (as good as Opera's ;-) EXCEPT for any <select> elements, which are...
4
2822
by: John Fereira | last post by:
So, one of the limitations of multipart-form handling is that when an <input type="file" ..> tag is used it will bring up a window which allows a user to select a file for upload but won't allow the user to select multiple files. As you may know, the tag produces a text input field with an adjacent button for selecting a file from the local file system. As I would like to be able to allow the user to upload an arbitrary number of files...
7
1911
by: sean | last post by:
SELECT drop-list width incorrect for long text when SELECT is fixed width Setting the width style of a SELECT statement causes the contents of the drop-list to be cropped. This is problematic when attempting to limit the width of the select box for layout purposes (say, a fixed-dimensions application UI) whilst still presenting the user with all of the required information to make a choice. Any clue to solve the above issue is highly...
0
1467
by: Jeremie | last post by:
I'm working on a page. I've put a select dropdown and a span on the page. When the user selects an option in the dropdown, it performs an async callback to populate the span. The code is simple, and I get the expected effect on IE and FireFox, but not on Safari... and IE is firering a read error exception (The program tried to read at addresse 0xxxxxxxx) when closing !! So I did several tries... First of all, I tried replacing the...
4
5809
by: =?Utf-8?B?c2lMdmVy?= | last post by:
Hhi, I'm working on an asp .net project that have small side panel which 'disallow' long SELECT/dropdown list.. So i could not force them to a certain that shows my longest option, which now most of my option was being 'cut-off' half way, not able to show to full text.. Is there any way I could do some thing like in e Mozilla FireFox (as currently using IE), which 'opens' e dropdown list when being click and shows e full text that...
9
11842
acoder
by: acoder | last post by:
Don't you just hate it when you've got something working in every browser (that you've tested on) except the XYZ browser? It would be a good idea to document some strange or incorrect behaviours of particular browsers with possible solutions and workarounds. These will be documented in the following format: Problem Give a brief description of the problem Browser Which browser it affects (include version number if applicable)
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10211
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9994
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8870
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7408
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.