Put your message here! Contact me for more information
 
 








 

I spent a few hours yesterday and today working on a small AJAX page with multiple combo boxes that are populated from the database. I use Firefox 99% of the time and I utilize the excellent YUI library for cross-browser compability. I almost done with the page (I’m pretty proud of it too) so I decided to show it to my work buddy. On IE (both 6 and 7), my beatiful double combo boxes just failed miserably. A part of what I was doing was to populate a double combo box with dynamic data from the database. I created an empty SELECT tag (dynamically via JS), assigned its an ID, made an AJAX request, then populated the empty SELECT tag via the innerHTML with the AJAX response, which is a list of OPTION elements. On Firefox, the double combo works so well that I thought it was time wrap up the project. However, on IE, the combo box just mysteriously failed to get populated.

First of all, I tried to eliminiate the possibility of my AJAX requests got messed up somewhere. I traced through the flow of the functions and put alert() to assert the responses of the AJAX requests as I was afraid that the IE 7 I was using to test the page is not compatible with the YUI’s Connection Manager.  This was also the first time I ran IE7 after deciding to install it the other day.  IE7’s AJAX feature turned out to work just fine, however, after several checkpoints using alert(), I finally discovered a bug in how IE handle the innerHTML property of select item: If the select item’s innerHTML propety is set programmatically, IE will truncate the first opening option tag.

In my case, what I got back from the AJAX request was a list of OPTION elements. When I do the assignment to the innerHTML of the empty SELECT element, the OPTION list’s HTML code got truncated, specfically the first opening OPTION tag. Hence, the html code I used to populate the select list was malformed. And thanks to IE’s impotency, I could not see the generated HTML code of the page (which in Firefox, the Web Developer toolbar works nicely) so I could not even debug HTML source.

IE7 BugsI did some Google lookups on IE select element’s innerHTML bug and found out that it is a confirmed bug by Microsoft, moreover, it’s been confirmed since IE 5 (Microsoft’s link). That was so 1999 and yet the bug still lives on until now with IE7. Humh, I totally understand that fixing bugs is being taken care seriously at Microsoft. But the innerHTML bug should be trivial to fix (no security risks that I can think of) and it’s been there for years without anyone from Microsoft bothering to do anything. It’s just ridiculous.

Oh there’s more, the Microsoft Bug page (the above link) shows that the bug report is at revision 2.0. Yeah, I thought “bug 1.0″ is more than enough but apparently I’m wrong because it’s Microsoft.

-1 to microsoft for the slowness of *TRIVIAL* bugs fixing.

-1 to micrsoft for the lack of developer’s handytools. Study Firefox and how the community are doing it, please.  (I do aware of the Developers Toolbar for IE, but it is not nearly as useful as the Firefox Web Developers Toolbar)


 

30 Responses to “Bug Day: Internet Explorer 7 SELECT element innerHTML bug



JR
5:49 am
November 30, 2006
#2806

Thanks. This explained a bug I have been trying to work on for a couple of days.

I am using prototype to have a series of comboboxes tied together. One selects a country, next the region, next the city. It works like a charm in mozilla, but ie, no dice, unless I wrap the combo’s under divs. Now I have to figure how to destroy elements in mozilla so then prototype does not end nesting innerhtmls.

I am new on this stuff, but it has been an uphill battle with these Redmond dudes….

Regards




Garrett
2:40 pm
December 12, 2006
#5089

Greetings:

This is the same bug I’ve come across today. If people are looking for a work around, I found a simple solution. On the servlet page where you build the list of options, include the entire select input statement.

The following example is coded in coldfusion, but can be adapted to any language.

For example, instead of using:

foo
bar

Use this:

“>
foo”>
bar”>
“>

And finally, output the variable on the servlet page:

strSelectStatement

I hope this helps.




Olivier Voutat
6:57 pm
December 15, 2006
#7057

I did a little different from Garret. (Garret ?)

I receive the options string from the server and insert with the innerHTML as this.

document.getElementById(”div_dest”).innerHTML = “” + options + “”

reading a little more about it there is a little more complicated way, using the create element for new options tag and appendChild to insert in the select tag present in the page.




Olivier Voutat
3:37 pm
December 16, 2006
#7472

Still trying to use the hard way of creating options elements and appending them, FF ok but ie now, this IE is really a crap. Microsoft is about marketing only. Quality NONE.




Olivier Voutat
8:39 pm
December 17, 2006
#7885

I have some fantastic news people, after a lot of reading (AND VERY BIG HEADACHE) I found a universal solution (tested in IE and FF, and looks like it works in Opera too)

var opt = document.createElement(”option”);
opt.innerHTML = “text of the option”;
opt.value = “0″;
document.getElementByID(”mysel”).appendChild(opt);

Yeah, ridiculous isn’t it? opt.innerHTML instead of opt.text. You can do too

opt.appendChild(document.createTextNode(”text”));

but why would you want a longer string?




Olivier Voutat
7:27 am
December 18, 2006
#8053

After some thinking my final vote will go for my original solution. Do innerHTML with the complete select.

Reason: speed, if you introduce only a string into your code it is really faster. But keep the appendChild in mind for special cases.




chris
10:43 am
December 20, 2006
#9498

Select.appendChild can crash IE5 Windows so…




bart
2:47 pm
December 21, 2006
#9783

Has anyone ran into the problem where the select breaks if you put an event handler on it? For example, I added an inline onclick=”highlight()” just to change the background color of the active field. In order to select anything you first have to double-click the select for it to open and then select. Same thing when you use onfocus. Apparently, you have to use onfocusin for the select to behave correctly.




Olivier Voutat
6:35 pm
December 28, 2006
#11259

Had the same kind of trouble bart. Use onkeyup (for keyboard selection) and onchange (for mouse selection) and you will not have this kind of trouble again.




7:42 am
January 26, 2007
#14611

Sadly, this isn’t the only thing they haven’t patched. Have you ever tried to replace the innerHTML of a TBODY tag?

Forget it…




Bob
5:21 pm
February 4, 2007
#15643

Hi, I’m having a related but different problem with innerHTML and IE7. It doesn’t involve a select element at all. The problem occurs several places throughout our app, but the simplest version is found in a page that has the following code:

var seemsToHangPainted = false;
var seemsToHangOut = '
+ ‘If your browser seems ‘
+ ‘to hang
, wait at least two minutes before ‘
+ ‘declaring it hung. The system sometimes pauses for a couple ‘
+ ‘of minutes to clean out garbage, and it won\’t do anything else ‘
+ ‘while it is cleaning house. ACE may require a particularly ‘
+ ‘long time to evaluate responses to mechanism questions.’;

function paintSeemsToHang() {
if (!seemsToHangPainted)
document.getElementById(’seemsToHang’).innerHTML = seemsToHangOut;
else document.getElementById(’seemsToHang’).innerHTML =

+ ‘If your browser seems to hang
‘;
seemsToHangPainted = !seemsToHangPainted;
} // paintSeemsToHang()

If
MarvinSketch® does not load properly

IE7 users complain that nothing happens when they click on the link. What can I do to fix the problem? BTW, everythign works fine in other browsers, including Firefox and Safari.




1:04 pm
February 6, 2007
#15848

@Bob

My WP doesn’t seem to like your code in the comments. I think you may have to do some debugging in IE to get this straightened out. If I were you, I would put an alert() at the beginning of paintSeemsToHang() and see if the method is being fired in IE. Also, instead of just having the href=”paintSeemsToHang()”, try href=”javascript:paintSeemsToHang();”, or even try this

a href=”#” onclick=”paintSeemsToHang();return false;”

If none works, then you may have to recreate all the elements and append them to the appropriate position in the DOM, instead of using the innerHTML property.

Good luck!




sujay
10:42 pm
March 2, 2007
#17622


8:41 am
March 9, 2007
#18073

http://www.goorbis.com - IE html source sniffer.

Drag the cursor on an IE page after you’ve altered it with JS, and it will show you what IE ‘thinks’ is there. It’s no developer toolbar, but it’s helped me solve a lot of IE’s crap.




S!ava
2:48 am
April 12, 2007
#23039

I think i found a solution, yes it’s not as it should be, but works and good. The thing is that i write the entire tag with new options - i get the outerHTML, remove the contents and add new options HTML code, then i assign new outerHTML code to Select control.

var dest = window.opener.document.getElementById(remctl);
if (dest != null)
{
var newOptions = new Array();
var outerTag = dest.outerHTML.replace(dest.innerHTML,’$');
var i;
for(i=0;i’ + dest.options[i].innerText + ”;
}

newOptions[i+1] = ‘ ‘ + selectText + ”;
dest.outerHTML = outerTag.replace(’$',newOptions.join(’ ‘));
}

Cheers!




4:40 pm
June 7, 2007
#37872

Oh, thank you for your post. I was trying to solve this problem for a half of a day, and maybe would continue thomorrow, if I wouldn`t read your post.
Damn sexplorer !!!




7:46 pm
August 13, 2007
#49540

in reply to Olivier Voutat’s comment…
onchange and onkeyup is Almost a complete solution.
onpropertychange has to be used for IE to get around its autocomplete nonsense.

http://krisgale.com/three-things-a-web-developer-might-do-to-force-feed-their-applications-to-internet-explorer-7/




9:12 am
December 14, 2007
#88334

In reference to the first problem with the selectlists and innerHTML command:
If you are able to check which browser your visitors are using in JS you can use a browser-specific javascript.
I solved it like this:

If the user has FireFox or some other browser:
document.getElementById(’SELECTLIST’).innerHTML = options;

If the user has Internet Explorer:
document.getElementById(’SELECTLIST’).outerHTML = “”+options+”"

Looks kind of crappy since what you’re doing is really to replace the called element with itself, but with the updated optionlist.

It works for me though.




MrRobot
5:11 am
December 18, 2007
#90018

HAH!

“View source” command is still bugged, which locks from time to time, till you clean history. It was ‘98 or something when I had first discovered it. 10 years of a stupid, big black bug, which travels through versions.

XP is still bugged, that you can’t load a theme file with ‘windows xp style’, which will make most of the application menus go strange, in other words, xp style themes just doesn’t work. No patches, no sp fixes, for, 7 years now?

Ok MS, you don’t bother making patches, but how can those obvious bugs stay unnoticed / unattended for that long? Unbelievable!




[…] So here comes my first reason why IE7 sucks (or IE sucks in general): IE doesn’t have firebug. It is a firefox plug which is used for front end debugging, well, I used it for debugging my code. It has a lot of nice features like DOM element inspecting, javascript debugging where you can set up break point, watch virables and etc. I used it so much that I don’t know what I do without firebug. So under IE7 I have to actually print out everything I want to figure out what went wrong. I suffered as someone who have been using eclipse and all of sudden he has to use printf + javac to debug. Luckily that process didn’t go very far: I found that every time I try to inject code like “<option>AAA</option>” into the innerHTML via my AJAX call, for the first option it always become something like “>AAA</option>”, no matter how you define it. I did some research online and I found the solution in this blog . The freaking bug is from IE7 itself, oh wait, there is more, do you know this bug has been sitting there for a fucking 5 years from IE5(in a different format, quote needed) and nobody in Microsoft has the guts(or the knowledge, appreantly) to fix it? This is so freaking awosome. Make it suck point two for IE7. I’ve done some additional homeworks for this issue but so far seems there are two ways to solve this and neither of them is perfect, one is to use appendChild() but it will crash IE5(I’m almost speechless at this point) and not very , while the other way is to use outerHTML, and it’s not that time efficient. I finally went with the appendChild() and asked user to use IE6+. Why? because it is relatively slightly easier to code (I’m a lazy guy) and the project is an internal application, there won’t be too much diversified users, plus most of them are QA engineers at Yahoo, they have all kinds of browsers installed on their compters. […]




Adam
8:49 am
January 30, 2008
#108589

I ran into this same problem, and similarly trying to dynamically change the options of a select with new option html. I went with a fairly simple regex route that seems to be working so far. This is working on FF, IE7, and Safari 3 beta.

var re = new RegExp(”(\)(.*?)(\)”, “i”);

$(child_id).innerHTML = transport.responseText;

if($(child_id).outerHTML)
$(child_id).outerHTML = $(child_id).outerHTML.replace(re, “$1″+transport.responseText+”$4″);

FF works with the .innerHTML command, but shows an undefined for .outerHTML, so the outerHTML command doesn’t work with FF. IE7 messes up the .innerHTML command, but works with the outerHTML command. Strangely, Safari returns the same outerHTML results as IE7, but I could only get it to work with the innerHTML command, so it was a mix of both.

FYI, I’m using prototype here, but you can change the $() to document.getElementById() if you want.




Adam
8:50 am
January 30, 2008
#108592

Looks like the comment parsed out the important bits of the regex. Another try.

var re = new RegExp(”(\<select(.*?)\>)(.*?)(\<\/select\>)”, “i”);




Peter
10:27 pm
March 6, 2008
#126257

i had same problem, and now i try appendChild(). Thanks alot




NabLa
9:49 am
March 13, 2008
#128563

Thanks Adam, that worked perfectly.




Amnamak
11:22 am
April 28, 2008
#148521

I am using selectBoxOject.options[selectBoxOject.options.length] = new Option(i); . Very simple. See: http://www.devguru.com/Technologies/ecmaScript/quickref/option.html




2:34 pm
June 20, 2008
#171896

Just checked a couple days ago that this bug is still present in IE8 bêta…




1:01 am
July 24, 2008
#187743

It seems (IE7) also that some table elements are not changed by innerHTML, at least tr seems not work. Only safe elements seem to be such as div and span…??




1:59 am
July 24, 2008
#187768

Today in IE7 I am trying to create a select dynamically and then insert an optoin based on user input. I am using the same code for dynamically created selects and selects in the original page HTML. Well, with selects that were not dynamically created it works fine, even in IE. But for the dynamically created select, which has a few options put in it when its created, what happens is that when another option added, using “insertBefore” method, on IE the select “length” property then changes to zero! When I changed selectedIndex, that single option will show in the combo, but it won’t drop-down (length 0). I haven’t solved it yet, i guess I will put the select into a div, and when I add another option, I’ll just recreate the entire select…. Of course, this is working fine already in all other browsers I’ve tested. Bizarre, this appears to be an IE bug specific to seletcts that were creatd using DOM methods.




2:10 am
July 24, 2008
#187772

Olivier Voutat, your method is the way I am adding options to my select. I used cloneNode(true) to clone an entire row of a table, appended it to the tbody. the table row has a select in it … when i used this method of adding options on the select in the dynamically added table row, it failes in * IE7 * (length goes to zero) .. but it does work on selects elsewhere, which happen to be non-dynamic selects in the page HTML. whether this is specific to selects created through this “cloneNode” process in a table, or all dynamically created selects, i don’t know.




Max
3:19 pm
July 28, 2008
#190059

You will find solutions to all your IE bugs here:

http://webbugtrack.blogspot.com/search/label/innerHTML

the Select element is only one of the elements in IE that doesn’t work properly with the DOM methods.




 

Leave a Reply