Jeepstone
Jeepstone

Reputation: 2601

JQuery .filter() in IE8

It looks like IE8 doesn't support the Jquery .filter() method - Why won't .filter() work in Internet Explorer 8?

I have the following code which filters a dropdown list

if($('#deliveryPostcodeEstimator').length > 0) {
        $('.shippingregionselector').hide();
        $('#deliveryPostcodeEstimator')
            .blur(function() {
                //Set to default
                $('select[name=country] option:last').prop('selected', true);
                //var defaultPostcode = 'GL50';
                //$("select[name=country] option").filter(function() {
                //  return $(this).text() == defaultPostcode; 
                //}).prop('selected', true);
                //Set to matching postcode value if set
                $('select[name=country] option').filter(function(index) { 
                    return ($(this).text() == $('#deliveryPostcodeEstimator').val().toUpperCase().substring(0,4).trim()) 
                }).prop('selected', true);
                //Submit
                var thisForm = $(this).closest("form");
                thisForm.submit();
            })
            .keyup(function() {
                $(this).val($(this).val().toUpperCase());
            });
        $('button.pcodechange').click(function() {
            var thisForm = $(this).closest("form");
            thisForm.submit();
        });
    }

The problem line is

return ($(this).text() == $('#deliveryPostcodeEstimator').val().toUpperCase().substring(0,4).trim()) 

Which gives the following error

Object doesn't support this property or method How do I 'wrap this in an object' as suggested in the previous post?

Thanks

Upvotes: 3

Views: 2703

Answers (2)

WickedSunny
WickedSunny

Reputation: 1

IE9 and onwards support .filter()

Here is what you can do: Define your own Filter.

if (!Array.prototype.filter) {
  Array.prototype.filter = function (fun /*, thisp */) {
    "use strict";

    if (this === void 0 || this === null)
        throw new TypeError();

    var t = Object(this);
    var len = t.length >>> 0;
    if (typeof fun !== "function")
        throw new TypeError();

    var res = [];
    var thisp = arguments[1];
    for (var i = 0; i < len; i++) {
        if (i in t) {
            var val = t[i]; // in case fun mutates this
            if (fun.call(thisp, val, i, t))
                res.push(val);
        }
    }

    return res;
};
}

Upvotes: 0

jantimon
jantimon

Reputation: 38150

Your error is probably because of your .trim() call.

String.prototype.trim is not available for Internet Explorer 8 according to this table: http://kangax.github.com/es5-compat-table/

You could use jQuery.trim instead:

 var search = $.trim($('#deliveryPostcodeEstimator').val().toUpperCase().substring(0,4));
 $('select[name=country] option').filter(function(index) { 
   return $(this).text() == search; 
 }).prop('selected', true);

You could also use a pure javascript solution which is described in cernunnos link:

.trim() in JavaScript not working in IE

Upvotes: 5

Related Questions