0

I'm trying to make a simple js dropdown function. Basically, I have a few elements w/ the nb-dd class, and need to assign a click event to them. On click they will get an additional class.

when clicking not on an item with nb-di class then I remove the dd-o class from the element that had the event.

When I run the code now it only fires on the first click... the code is pretty simple, but I'm not sure what I'm missing :(


var n = document.getElementsByClassName('nb')[0],
    dd = n.getElementsByClassName('nb-dd');

function m() {
    var l = this;
    l.classList.toggle("dd-o");

    window.addEventListener("click", h);
}

function h(e) {
    if (!e.target.matches('.nb-di')) {
        for (var q = 0; q < dd.length; q++) {
            if (dd[q].classList.contains('dd-o')) dd[q].classList.remove('dd-o');
        }
    }
}

for (var c = 0; c < dd.length; c++) dd[c].addEventListener('click', m);

any suggestions are appreciated! Thanks!

sur.la.route
  • 440
  • 3
  • 11

0 Answers0