0

I am using IE8 and am receiving an error of “object not found” with the line:

var l_classList = $(this).attr('class').trim().split(/\s+/);

The actual code block is as follows:

$( "div.panel" ).each(function(index) {
    var l_classList = $(this).attr('class').trim().split(/\s+/);
    if ( l_classList.length == 1 ) $(this).addClass("panel-default");
});

Any ideas of how to get around this within IE8?

halfer
  • 19,824
  • 17
  • 99
  • 186
tonyf
  • 34,479
  • 49
  • 157
  • 246
  • Have you tried `$(this).prop("className")`? (Or just `this.className`?) – Pointy Feb 18 '15 at 23:39
  • Tried both your suggestions and unfortunately no luck. – tonyf Feb 18 '15 at 23:49
  • What line of code is giving you that error? (Also, is it really "object not found"? That's a weird error.) Their might be no `.trim()` function on the String prototype in IE8. – Pointy Feb 18 '15 at 23:49
  • What are you trying to achieve here? Looks like you are adding the class `panel-default` to each div element that has only one class set, and since you used `.panel` as selector already, that class could only be `panel` in any case? Just checking if the class _is_ `panel` (after trimming the value, should your environment make that necessary) seems a little more direct than the hoops you’re jumping through … – CBroe Feb 18 '15 at 23:59
  • try: `$.trim($(this).attr('class')).split(/\s+/);` ([source](http://stackoverflow.com/a/3439327/2387772)) – yuvi Feb 19 '15 at 00:42

1 Answers1

0

IE8 doesn't support the trim() method.

Try adding this to your helper functions and then run your script.

if (!String.prototype.trim) {
    (function() {
        var rtrim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;
        String.prototype.trim = function() {
            return this.replace(rtrim, '');
        };
    })();
}
tkore
  • 1,101
  • 12
  • 31