cluster1
cluster1

Reputation: 5674

JavaScript: How can I check for "HTMLUnknownElement"?

I have made a function for to create DOM-elements:

function getHtmlElement(nodeName, attrs, innerHtml) {
  var ret = null;

  if (!nodeName || typeof nodeName !== 'string') {
    throw { message: 'No nodeName-parameter assigned.' }
  }

  attrs = attrs || '';
  innerHtml = innerHtml || '';

  ret = document.createElement(nodeName);

  attrs = attrs.split(/\s/g);

  if (attrs[0]) {
    attrs.forEach(function(attr) {
      attr = attr.split(/=/);

      if (attr[1]) {
        ret.setAttribute(attr[0], attr[1]);
      }
    });
  }

  ret.innerHTML = innerHtml;		

  return ret;
}

The type of DOM-element is specified as the first parameter of the function.

For example: div, li, p, h1 ...

Now I would like to check if the creation of the element has worked. Because there could be a typo (diiv) or complete nonsense assigned as the parameter.

I know that there exists something called "HTMLUnknownElement" which I can inspect after the element creation. But I don't know how.

Is HTMLUnknownElement a property of a new created DOM-element?

How do I check for HTMLUnknownElement?

Upvotes: 0

Views: 2040

Answers (1)

Sebastian Simon
Sebastian Simon

Reputation: 19485

You can check whether the created element is an instance of HTMLUnknownElement like this:

ret instanceof HTMLUnknownElement

Upvotes: 4

Related Questions