I have a code, but I need help please, how can I close the menu when clicking on a link item. that’s what I’m stuck on
JavaScript
x
17
17
1
$('.burger').on('click', function() {
2
$(this).toggleClass('active');
3
$('.menu__list').slideToggle();
4
});
5
6
let ul = document.querySelector('.menu__list');
7
let li = document.querySelectorAll('.menu__link');
8
9
li.forEach(el => {
10
el.addEventListener('click', function() {
11
ul.querySelector('.menu__link--active').classList.
12
remove('menu__link--active');
13
14
el.classList.add('menu__link--active');
15
});
16
});
17
Advertisement
Answer
Just add .slideToggle() to the click listener:
JavaScript
1
18
18
1
$('.burger').on('click', function() {
2
$(this).toggleClass('active');
3
$('.menu__list').slideToggle();
4
});
5
6
let ul = document.querySelector('.menu__list');
7
let li = document.querySelectorAll('.menu__link');
8
9
li.forEach(el => {
10
el.addEventListener('click', function() {
11
ul.querySelector('.menu__link--active').classList.
12
remove('menu__link--active');
13
14
el.classList.add('menu__link--active');
15
$('.menu__list').slideToggle();
16
});
17
});
18