Array.indexOf throws errors in some IE versions

IE7 and IE8 are not letting me splice my array (Safari, Chrome, Firefox work):

        lzaCreateAd1.weatherArray = new Array();
        var jWeatherIcon = $('.weatherIcon');

        jWeatherIcon.bind('click', function (){
            var targetID = $(this).attr('id') + 'Box',
            idVal = targetID.substr(5,1);

            var jTargetBox = $('#'+targetID);

            if (jTargetBox.hasClass('inactive')) {
                jTargetBox.removeClass('inactive').addClass('active');
                lzaCreateAd1.weatherArray.push(idVal);
            } else if (jTargetBox.hasClass('active')) {
                jTargetBox.removeClass('active').addClass('inactive');
                lzaCreateAd1.weatherArray.splice(lzaCreateAd1.weatherArray.indexOf(idVal),1);
            }
        });

IE throws the following error: "Object doesn't support this property or method" for this line:

lzaCreateAd1.weatherArray.splice(lzaCreateAd1.weatherArray.indexOf(idVal),1);

Any ideas? Or other ways to remove an array item by value? Thanks in advance!

Answers


Array.indexOf is not supported by Internet Explorer before version 9. You can use the jQuery's $.inArray utility function, or any other shim/polyfill you want instead.

lzaCreateAd1.weatherArray.splice($.inArray(idVal, lzaCreateAd1.weatherArray) ,1);

See: http://api.jquery.com/jQuery.inArray/


Need Your Help