I am trying a simple dropdown on a vue project. But the dropdown doesnt drop when I click on it. I have put the console inside the function. The message gets displayed on console that means the function is being called. But it doesnt work.
<template>
<div class="dropdown">
<button @click="toggleDropdown" class="dropdown-button">
{{ selectedOption || "Select an option" }}
</button>
<ul v-if="isOpen" class="dropdown-menu">
<li v-for="option in options" :key="option" @click="selectOption(option)">
{{ option }}
</li>
</ul>
</div>
</template>
<script>
export default {
name: "DropDown", // Adding name to the component
data() {
return {
isOpen: false,
selectedOption: null,
options: ["Option 1", "Option 2", "Option 3"],
};
},
methods: {
toggleDropdown() {
console.log("Button clicked");
this.isOpen = !this.isOpen;
},
selectOption(option) {
this.selectedOption = option;
this.isOpen = false;
},
},
};
</script>
<style scoped>
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-button {
padding: 10px;
background-color: #3498db;
color: white;
border: none;
cursor: pointer;
}
.dropdown-menu {
position: absolute;
background-color: #f9f9f9;
border: 1px solid #ddd;
list-style: none;
padding: 0;
margin: 0;
width: 100%;
}
.dropdown-menu li {
padding: 10px;
cursor: pointer;
}
.dropdown-menu li:hover {
background-color: #ddd;
}
</style>