I have some jQuery code which works great in Firefox and Chrome, but does not work in Internet Explorer 8 (haven’t tested in other IE versions). Internet Explorer is running in standards mode.
The intent of the code below is to create a new list item on the fly. Its content is set to that of a form that is inside of a hidden section element. What happens when using IE is that the section element becomes unhidden and the li is added to the list, but is empty.
What’s wrong with this code in IE?
$('ul.elementlist').on('click', '.add-element-icon', function (event) {
var plusIcon = $(this);
plusIcon.hide();
var parentLi = plusIcon.parent();
var before = parentLi.attr('id');
var after = parentLi.next().attr('id');
if (typeof after === "undefined") {
after = 'none';
}
var li = $('<li class="element">').html($('section.add-element').html());
$('input[name="elementBefore"]', li).val(before);
$('input[name="elementAfter"]', li).val(after);
li.insertAfter(parentLi);
});
Since you are using the
sectionelement I would guess that you are making use of other HTML5 elements and are using an HTML5 doctype:<!DOCTYPE html>. Under these assumptions I would assert you need to do a few things to make your markup “IE friendly.”First ensure that IE truly is running in IE8 Standards compatibility mode* by adding the following meta tag to your head section:
Immediately following the standards compatibility setting add a reference to html5shiv which is a JavaScript shiv for IE to recognize and style HTML5 elements:
Between these two changes you should be good to go. After the changes force IE to do a full resource refresh by pressing CTRL + F5.
* If this is a new web site you may want to consider using
IE=edgeas it tells Internet Explorer to use the highest mode available.