user1049057
user1049057

Reputation: 459

className null or not an object in IE8

I am running the below code in IE8 and I am getting className is null or not an object. Any help

JS:

var validators = {
    allowalphanum: function (val) {
        return /^[a-z0-9]+$/i.test(val);
    },
    allownospace: function (val) {
        return !/\s/.test(val);
    }
}

$('#submit_form .required').blur(function () {
    var returnVal1 = true;
    var classes = $(this).className.split(/\s+/);
    for (var p in classes) {
        if (classes[p] in validators) {
            returnVal1 = returnVal1 & validators[classes[p]](input.val());
        }
    }
});

Upvotes: 1

Views: 1093

Answers (3)

Emil A.
Emil A.

Reputation: 3445

Use either vanilla js:

var classes = this.className.split(/\s+/);

or use jQuery method attr:

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

Upvotes: 2

Saturnix
Saturnix

Reputation: 10564

var classes = $(this).className.split(/\s+/);

I don't recall className being a valid jQuery method. .attr('class') is probably what you need if you want to use the jQuery object.

Other answers has already been posted on how to do that without jQuery.

$(this) differs from this as the first one gives you a jQuery object, the second a DOM object.

Upvotes: 1

palaѕн
palaѕн

Reputation: 73906

Just replace your code:

var classes = $(this).className.split(/\s+/);

with this:

var classes = this.className.split(/\s+/);

Actually className is a DOM property. It's not a property of jQuery object.

Upvotes: 3

Related Questions