Home > database >  How to add instance of javascript Class in a DOM element
How to add instance of javascript Class in a DOM element

Time:04-01

I need to expose the istance of a class to be called from the dom element.

i've created a class that i'll instance as new myCustomClass(). This class is related to a DOM element.

What i need to do in my class to access to his methods and property form the dom?

I need something like this (this is something that swiper.js do)

var foo = document.querySelector('.element-where-class-is-instanced').myCustomClass;
foo.init()

thanks to all

CodePudding user response:

Just assign the class instance to the element property earlier:

document.querySelector('.element-where-class-is-instanced').myCustomClass = new myCustomClass();
  • Related