11 May 2008

Why I hate innerHTML

Why I hate innerHTML? Because IE suck! It doesn't work for select tag in IE. I have something like

<select id="foo"><option value="1">baz</option></select>

Javascript that doesn't work in IE:
document.getElementById("foo").innerHTML = '<option value="2">baz</option>';

Solution -- have to use DOM lah! Note that use innerHTML will destroy the tag events. My prefered solution:

document.getElementById("foo").options[0] = new Option("baz", 2);

Astute reader may ask, why not use dom method add() ? Again, because IE suck!

var x=document.getElementById("foo");
x.add(y,null); // standards compliant
x.add(y); // IE only


More love/hate on innerHTMl, http://www.wait-till-i.com/2006/04/18/innerhtml-vs-dom-pot-noodles-vs-real-cooking/
Post a Comment