Need a quality coder? The author of this blog is now freelancing:
Freelance PHP/CakePHP Web Developer Bristol / Southwest UK

Don’t trust the output of innerHTML() or jQuery.html() in Internet Explorer

16 May
2010

Especially if you’re trying to match attributes and their values!

Thankfully Kooilnc, the godsend has a solution:

http://stackoverflow.com/questions/1231770/innerhtml-removes-attribute-quotes-in-internet-explorer

I’ve wrapped that up into a jQuery function here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
$.fn.ieInnerHTML = function() {
  var zz = this.html(),
       z =
     zz.match(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/g);
    if (z){
      for (var i=0;i<z.length;i++){
        var y, zSaved = z[i];
        z[i] = z[i].replace(/(<?\w+)|(<\/?\w+)\s/,
                            function(a){return a.toLowerCase();});
        y = z[i].match(/\=\w+[?\s+|?>]/g);
         if (y){
          for (var j=0;j<y.length;j++){
            z[i] = z[i].replace(y[j],y[j]
                       .replace(/\=(\w+)([?\s+|?>])/g,'="$1"$2'));
          }
         }
         zz = zz.replace(zSaved,z[i]);
       }
     }
    return zz;
}

// Usage:
$('#getInnerHtmlOfThis').ieInnerHTML();

Comment Form

top