0

I feel like a noob, but I haven't worked that much with JS objects. I want to create a object so I can apply some custom functions to the DOM elements.

Sorry for being unspecific. I have ppdated my questions with more complete picture of my code.

    menuItemArr = navBar.querySelectorAll('.kx-menu__link');

    [].forEach.call(menuItemArr, function(menuItem) {
        menuItem.addEventListener('click', function(e) {
            e.preventDefault();
            toggleMenuItem(this);
        });
    });

    function toggleMenuItem(menuLink){
        let obj = obj_menuItem(menuLink);
        obj.showClasses();
    }

    let obj_menuItem = function(e){
        classes = e.classList;
        function showClasses() { console.log(classes); }
    };

results in TypeError: obj is undefined

Steven
  • 19,224
  • 47
  • 152
  • 257
  • You shouldn't create custom methods to the DOM elements at all, it's considered as a bad practice. If you'd introduce a use-case, we might find a better solution. – Teemu Feb 10 '20 at 14:39
  • What exactly is **menuListItem**? – obscure Feb 10 '20 at 14:41
  • You could create a custom-element which extends the functionality of the `HTMLElement` interface. And give that custom-element it's own methods for you to use. – Emiel Zuurbier Feb 10 '20 at 14:44

0 Answers0