3

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

Community
  • 1
  • 1
Jeepstone
  • 2,591
  • 5
  • 22
  • 38
  • 3
    jQuery.filter and array.filter are different things – Dakait Mar 08 '13 at 10:53
  • `It looks like IE8 doesn't support the Jquery .filter() method` Do you get an error or any warning message in the console in the browser debugging tools? Have you run it in Chromer/FireFox as well? Have you tried making a [**fiddle**](http://jsFiddle.net) to check if it all works there? – Nope Mar 08 '13 at 11:00
  • OK. So perhaps my question should be more along the lines of 'How can I get this to work in IE8?' It's fine in Chrome/FF – Jeepstone Mar 08 '13 at 11:01
  • The error is “Object doesn't support this property or method” – Jeepstone Mar 08 '13 at 11:01
  • What version of jQuery are you using? – Anthony Grist Mar 08 '13 at 11:05
  • @Jeepstone: Does the message say which method? Theoretically it could be any of the ones in the line `.text()`, `.val()`, `.toUpperCase()`, `.substring()` or `.trim()` Can you make each action a separate line assigning the result to a variable? that way it would propably be easier to figure out which command is causing the problem. – Nope Mar 08 '13 at 11:05
  • Hi, I'm using 1.8.3. François, the trim() could be a good shout. I'll split the lines out and see where it breaks. – Jeepstone Mar 08 '13 at 11:06
  • Looks like it's the trim(). – Jeepstone Mar 08 '13 at 11:12

2 Answers2

5

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

Community
  • 1
  • 1
jantimon
  • 36,840
  • 23
  • 122
  • 185
  • +1 Nice find. Also states that in the MDN Documentation under [**trim() - Browser Compatibility**](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/Trim#Browser_compatibility) – Nope Mar 08 '13 at 11:09
  • This appears to be the correct answer, and this http://stackoverflow.com/questions/2308134/trim-in-javascript-not-working-in-ie might be useful to resolve the issue – cernunnos Mar 08 '13 at 11:11
0

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;
};
}