T
T
TuMko2020-12-03 18:40:16
JavaScript
TuMko, 2020-12-03 18:40:16

Why can't I hang event on dropdown-backdrop in Bootstrap?

Hello. There is a dropdown on Bootstrap3. When it is opened, an empty element with the dropdown-backdrop class appears, by clicking on which this dropdown closes. I need that after closing the dropdown, a certain action is also performed, for example $('body').removeClass('noscroll'). But when I hang click $('.dropdown-backdrop').on('click', function () {}), it doesn't work. Please help me solve this problem.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yuri, 2020-12-03
@cheeroque

Most likely, when you hang the handler, the element simply does not exist, because the bootstrap only adds it after the dropdown opens. Attach a handler to document:

document.addEventListener('click', (event) => {
  if (event.target.classList.contains('dropdown-backdrop') {
    // нужный код
  }
})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question