Unfortunately, there is no way to determine how the tag was written in the code, because when you run JavaScript, the HTML code is already parsed for DOM objects.
However, your question seems to be more about whether a particular type of element can contain text. This simple test will give you an answer to the type of element:
function canElementContainText(tagname) { try { var e = document.createElement(tagname); return e.outerHTML.indexOf("/") != -1; } catch (ex) { return false; } }
For example, canElementContainText("div") returns true and canElementContainText("img") returns false .
You can then pass the tagName property of any element to this function to check it.
var result = canElementContainText(myElement.tagName);
wizulus
source share