Micanio
Micanio

Reputation: 67

JQuery and Internet Explorer - JQuery not working

I have a little script that replaces text with images depending on the value in a Wordpress custom field. It works perfectly fine in all browsers apart from IE.

My code is:

$(document).ready(function() {
$('.CustomRow').each(function(i, e) {
    if (m = (text = $(e).text().trim()).match(re = /: +(yes|no)$/)) {
        var class = $('span', e).text().replace(/[^\w]/g, '').toLowerCase();
        $(e)
            .addClass(class)
//              .text(text.replace(re, ''))
        ;
    }

});
if ($('.levelparking').html().match(/yes/)) {
    $('.levelparking').html('<img src="/wp-content/themes/directorypress/thumbs/level_ground.jpg">');
} else {
    $('.levelparking').hide();
    }

if ($('.water').html().match(/yes/)) {
    $('.water').html('<img src="/wp-content/themes/directorypress/thumbs/water.jpg">');
}else {
    $('.water').hide();
    }

 if ($('.greywater').html().match(/yes/)) {
    $('.greywater').html('<img src="/wp-content/themes/directorypress/thumbs/grey_water.jpg">');
}else {
    $('.greywater').hide();
    }

 if ($('.blackwater').html().match(/yes/)) {
    $('.blackwater').html('<img src="/wp-content/themes/directorypress/thumbs/black_water.jpg">');
}else {
    $('.blackwater').hide();
    }

 if ($('.electric').html().match(/yes/)) {
    $('.electric').html('<img src="/wp-content/themes/directorypress/thumbs/electricity.jpg">');
}else {
    $('.electric').hide();
    }

if ($('.extranight').html().match(/yes/)) {
    $('.extranight').html('<img src="/wp-content/themes/directorypress/thumbs/xtra_night.jpg">');
}else {
    $('.extranight').hide();
    }

if ($('.dogwalks').html().match(/yes/)) {
    $('.dogwalks').html('<img src="/wp-content/themes/directorypress/thumbs/dog_walks.jpg">');
}else {
    $('.dogwalks').hide();
    }

if ($('.realales').html().match(/yes/)) {
    $('.realales').html('<img src="/wp-content/themes/directorypress/thumbs/real_ales.jpg">');
}else {
    $('.realales').hide();
    }

    if ($('.busstop').html().match(/yes/)) {
    $('.busstop').html('<img src="/wp-content/themes/directorypress/thumbs/bus_stop.jpg">');
}else {
    $('.busstop').hide();
    }

    if ($('.wifi').html().match(/yes/)) {
    $('.wifi').html('<img src="/wp-content/themes/directorypress/thumbs/wifi.jpg">');
}else {
    $('.wifi').hide();
    }


});

I'm getting the following error message in IE Developer Tools:

Expected Identifier - Line 4, character 17.

This relates to this line:

var class = $('span', e).text().replace(/[^\w]/g, '').toLowerCase();

Any idea why this is happening?

Thanks in advance

Upvotes: 0

Views: 328

Answers (4)

Guffa
Guffa

Reputation: 700262

class is a reserved keyword in Javascript (ECMAScript), use a different variable name.

The reason that it works in some browsers is that they allow some keywords as identifiers in some situations. Naturally you should not use keywords as identifiers at all.

Upvotes: 3

wildpeaks
wildpeaks

Reputation: 7392

"var class" is most likely the problem because class is a reserved keyword in IE.

The solution is to use another variable name.

Upvotes: 3

Aliostad
Aliostad

Reputation: 81660

$('span', e) is null hence it will not work,. Post HTML or a jsfiddle sample.

Upvotes: 0

cjstehno
cjstehno

Reputation: 13984

My bet would be on the regular expression. You may want to dig into IE's regular expression support and make sure that it is not slightly different from the other browsers... as is often the case with IE.

Upvotes: 0

Related Questions