Alex Angas
Alex Angas

Reputation: 60027

Why is jQuery each() not firing with Internet Explorer?

I have a variable result with the following XML:

<Properties>
    <Property>
        <Name>Title</Name>
    </Property>
</Properties>

I'm then using jQuery 1.4.3 with each():

$('Property', result).each(function () {
    var name = $('Name', this).text();
    alert("Name: " + name);
});

For some reason this code isn't firing under IE8 however it works fine on Firefox 3.6 and Chrome 7. I've tried to find a bug report for this case but only found issues with older jQuery versions.

Any ideas?

Upvotes: 2

Views: 3038

Answers (3)

AlecTMH
AlecTMH

Reputation: 2725

In IE an xml string must be an object while the other browsers allow string type.

I had the same problem, I was getting xml data with ajax and each didn't work in IE8 until I added data type in ajax function:

$.get('http://url', {'a': 0, 'b': 1}, function(data) {), 'xml');

The above worked fine while the below failed:

$.get('http://url', {'a': 0, 'b': 1}, function(data) {));

Upvotes: 2

Alex Angas
Alex Angas

Reputation: 60027

This is caused by a bug in IE:

if ((properties.length == 0) && (jQuery.browser.msie)) {
    // IE screwing up
    var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.loadXML(result);
    result = xmlDoc;
    properties = $('Property', result);
}
properties.each(function () {
    var name = $('Name', this).text();
    alert("Name: " + name);
});

Good news - it doesn't occur in IE9. (Thanks to this SO answer).

Upvotes: 2

Alexey
Alexey

Reputation: 414

Why not use jQuery.XSLT plugin? It works great in all browsers where jQuery works I believe

Upvotes: 0

Related Questions