Monday, May 20, 2013

jQuery append() not work in IE 7, IE8

Shame the MS IE bad ass once again trying to break down the web developers...

When you using jQuery .apend() function in IE 7 or 8, It will throw a error and break the web page function down.

for example the code below will throw an error and stop to continue execute the rest of the code after this line:
    elm.append("<elm2></elm2>");

There is a example can solve this problem which is using XML DOM.

var strXml = "<root>"
                       + "<publish>"
                       + "    <store>"
                       + "        <book title=\"How to fix IE\"/>"
                       + "    </store>"
                       + "</publish>"
                       + "</root>";
         
// Build XML document
var xDoc = $.parseXML(strXml);

// create new child element using XML Document
var xBook = xDoc.createElement("book");

// setting the attributes of child element using XML DOM method 
xBook.setAttribute("title", "How to fix bad ass IE!");

// setting the attributes of child element using jQuery
$(xBook).attr("authr", "Bill ...");

// Append child element to some where in the XML Document
var xStore = $(xDoc).find("store");
xStore.append(xBook);