views:

36

answers:

1

I have some XML that I am parsing in jQuery.

<payload>
    <value key="VehicleMake">
        <value key="description">Aeon</value>
        <value key="code">18</value>
    </value>
    <value key="VehicleMake">
        <value key="description">Alfa Romeo</value>

        <value key="code">120</value>
    </value>
</payload>

In all browsers except IE (specifically IE8) the following works:

$.ajax({
    type : "GET",
    async : false,
    url : "/services/vehiclemake",
    success :
        function(xmlResponse) {
        var data = $("payload", xmlResponse).children("value").map(function() {     
            //IE8 fails on the next line
            var code = $(this).children("value [key='code']").text();
            var desc = $(this).children("value [key='description']").text();
            return {
                value : desc,
                vehicleCode  : code,
                label : desc
            };
        }).get();
    }
});

In IE8 it fails on the assignment to 'code' saying : "TypeError: Object doesn't support this property or method".

If I use this approach then it is fine:

$.ajax({
    type : "GET",
    async : false,
    url : "/services/vehiclemake",
    success :
        function(xmlResponse) {
        var data = $("payload", xmlResponse).children("value").map(function() {
            var code;
            var desc;           
            var elements = $(this).children();
            for (i = 0; i < elements.length; i++) {
                if(elements[i].attributes.getNamedItem("key").value == 'code') {
                    code = elements[i].text;
                }
                if(elements[i].attributes.getNamedItem("key").value == 'description') {
                    desc = elements[i].text;
                }
            }
            var desc = $(this).children("value [key='description']").text();
            return {
                value : desc,
                vehicleCode  : code,
                label : desc
            };
        }).get();

Can anyone tell me why the standard jQuery attribute selectors are not working in IE8? The XML is being returned with the correct mime type.

Thanks.

+1  A: 

I don't think you want a space here:

.children("value[key='code']")

Might not solve it but I think that needs fixing anyway.

Codesleuth
Perfect quick response. That was exactly it. Nice bit of cross-browser weirdness.
Damo
Excellent! I was unsure as I was posting from my phone, I couldn't source the info. I'm glad it helped :)
Codesleuth