-1

Is there a possible way via JavaScript to access a DOM Element via its constructor (preferably HTMLElement) through a custom property or just directly?

Something like the code below:

/* Some Attribute             
    document.body.someAttribute == document.body
        (this should be true)
*/

HTMLElement.prototype.someAttribute = (function() {
    /* Return the element. */
})();

EDIT

Apparently this was what I was asking:

Object.defineProperty(HTMLElement.prototype, "someAttribute", {
    get: function someAttribute() { return this }
});

document.body.someAttribute === document.body // -> true
Lapys
  • 936
  • 2
  • 11
  • 27
  • 1
    Not sure of the use case, but here is how you can select DOM elements. https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector – arodjabel Jul 28 '17 at 19:45
  • "access a DOM Element via its constructor" what are you trying to do with this? from how I see it, you won't have access to this method without the DOM element so providing a way to get itself from itself seems to be redundant, unless you're trying to access an attribute of the element, in which case [`Element.getAttribute()`](https://developer.mozilla.org/en-US/docs/Web/API/Element/getAttribute) exists – Patrick Barr Jul 28 '17 at 19:47
  • Possibly the same as [How to add my own methods to HTMLElement object?](https://stackoverflow.com/q/4670361/215552), but it's hard to tell... – Heretic Monkey Jul 28 '17 at 20:00
  • I don't know why this was closed. It is not unclear what they are asking. It is only clear that they are confused. It's obvious that the title can be changed to I something like, "How to add a method/property to a DOM element's constructor, so that the method can access the DOM element." ... This is why beginners can't ask questions on here. If the person had enough information to ask the question properly, they would not have a question in the first place. – Maiya Aug 30 '19 at 16:56
  • Here's the answer, since the question is closed: HTMLElement.prototype.myMethod = function(){ console.log("this element", this)} Then: let element = document.getElementById('my-element'). Then: element.myMethod() .... And the console will log your element, or do whatever you want do do in the method. – Maiya Aug 30 '19 at 16:59

1 Answers1

0

Your question is very unclear. Your title says "access", but the first example you give seems to be "testing". document.body is an instance of HTMLBodyElement, which is therefore the value of its constructor document.body.constructor, so

document.body.constructor === HTMLBodyElement

and

document.body instanceof HTMLBodyElement

and of course also

document.body instanceof HTMLElement

since HTMLBodyElement is a subclass of HTMLElement.

You cannot get from the constructor to the instance; no constructor has any idea what instances might have been created with it. To find instance(s) of a particular HTML element type, use document.querySelector[All](tagName).

  • Thanks, I guess I just wanted o know if it was possible to get the `constructor` from the instance. – Lapys Jul 29 '17 at 06:13
  • Further clarification of my answer in comments above: You don't want to get the constructor itself, you want to get the constructor's methods, which are stored on the constructor's .prototype object. If you add a method to the HTMLElement.prototype.myMethod = function(){ } ... then, call it like this element.myMethod() <----- because that particular element is calling the method, the "this" keyword will refer to the exact element that called the method. – Maiya Aug 30 '19 at 17:05