Objective
The main aim is to add a class to the dropdown element .menu-item-dropdown
whenever the user clicks on the .menu-item
element.
Issue
Currently, when clicking on the .menu-item
element, the returned value from the console.log
inside the showMobileNavDropdown()
function is null
. Upon clicking, the event.target
refers to .menu-item-main
instead of .menu-item
. This happens specifically when I click on the text of the li
. Otherwise, it works as expected.
What would be the most effective approach to include the text within the li
so that the .menu-item-dropdown
class can still be targeted?
Vue.js
<template>
<div>
<nav class="nav">
<div class="nav-left">
<img class="logo" src="/images/logo.svg" alt="" />
</div>
<div class="nav-center">
<ul class="menu-items">
<li
class="menu-item"
@click="showMobileNavDropdown($event)"
>
<!-- Main Title On Nav -->
<a class="menu-item-main" href="#">Company</a>
<!-- Dropdown -->
<div class="menu-item-dropdown">
<ul>
<li class="">
<a href="">About</a>
</li>
<li class="">
<a href="">Contact</a>
</li>
</ul>
</div>
</li>
</ul>
</div>
<div class="nav-right">
<a href="" class="btn">Contact us</a>
</div>
<div class="hamburger" @click="openMobileNav">
<span class="ham-line"></span>
<span class="ham-line ham-line-2"></span>
</div>
</nav>
<div class="mobile-nav"></div>
</div>
</template>
<script>
export default {
methods: {
openMobileNav() {
var mobileNav = document.querySelector(".mobile-nav");
mobileNav.classList.toggle("showMobileNav");
},
// This function
showMobileNavDropdown(event) {
console.log(event); // The element 'menu-item-main' is retrieved instead of 'menu-item'
console.log(event.target.querySelector(".menu-item-dropdown"));
},
},
};
</script>