Dennis
Dennis

Reputation: 297

Element is undefined Javascript error

Element.prototype.hasClassName = function (a) {
    return new RegExp("(?:^|\\s+)" + a + "(?:\\s+|$)").test(this.className);
};

can any ine tell me how to define this element

Upvotes: 1

Views: 958

Answers (2)

Jeremy J Starcher
Jeremy J Starcher

Reputation: 23863

JavaScript has a distinction between native objects -- those things created by JavaScript and host objects -- those things created by the browser for JavaScript to use.

The rules on how the two behave are quite different, but to summarize:

  1. Don't extend host objects.
  2. Don't try to access host objects in any way not advertised
  3. Don't trust them to behave.

Element is a host object, and under IE, doesn't follow the typical prototype behavior.

Upvotes: 0

Sirko
Sirko

Reputation: 74036

In modern browsers you could better rely on the classList attribute:

el.classList.contains( 'myclass' ); // returns true or false

For older browser MDN lists a polyfill.

Upvotes: 3

Related Questions