position: absolute is only applying to the initial item

I have a user card component with a menu button at the top that should display a menu when clicked. The issue I'm facing is that when I click on the menu button of the other user cards, the menu still shows on the first card instead of the one I clicked on. What could be causing this problem?

<template>
<div class="custom-users" v-if="users && users.length > 0">
      <user-card
        v-for="user in users"
        :key="user.userId"
        @open-menu="openMenu"
      >
      </user-card>
      <div class="menu" v-if="showMenu">
          <p>Delete</p>
      </div>
</div>
</template>
<script>
export default{
  data() {
    return {
       showMenu: false,
    };
  },
   methods: {
    openMenu(){
      this.showMenu = !this.showMenu;
    },
}
}
</script>
<style scoped>

.custom-users{
  position: relative;
}
.menu{
  position: absolute;
  height: 100px;
  width: 100px;
  top: 60px;
  right: 25px;
  z-index: 9999;
}
</style>

Answer №1

Elements that are absolutely positioned are positioned in relation to their nearest parent element that has the CSS property position: relative applied to it (or if the parent element also has position: absolute).

To ensure the dropdown is positioned correctly, both elements should be contained within a parent element that has the CSS property position: relative set so that the dropdown will be positioned in relation to the card itself, rather than the entire container.

Answer №2

The current menu is positioned relative to the element custom-users, which is shared among all instances of <user-card>. This creates the appearance that it is specifically associated with the first instance of <user-card> due to their alignment at the top left within the normal flow.

To address this issue:

<div class="menu" v-if="showMenu">
   <p>Delete</p>
</div>

Within the <user-card> component, apply position: relative; to its topmost element and integrate the following state and method into the <user-card> structure:

data() {
  return {
       showMenu: false,
    };
},
methods: {
   openMenu(){
      this.showMenu = !this.showMenu;
   },
}

Although currently not a major concern as the menu is displayed conditionally, if the menu evolves into a more substantial component, consider allowing it to be positioned based on coordinates passed in as props in the future.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Display and conceal button while scrolling in both directions

Seeking assistance with solving a coding challenge. I have created a function that reveals a button (an arrow pointing from bottom to top) when scrolling down. Now, I aim to implement another feature where the button appears after scrolling over 500 pixe ...

How about checking the memory usage in Javascript?

Similar Question: Looking for a Javascript memory profiler I am curious about determining the memory consumption of variables in JavaScript. Could it be done at all? ...

Transformed 700 audio players compartmentalized within a nested tab interface. Optimal tab coding techniques include jquery, ajax

We are currently working on developing a nested tab interface that will include 700 audio players for MP3 files all on the same page within various tabs. However, only one audio player will be visible at a time and will only appear when the tab is clicked. ...

Is it possible to incorporate async await with SetState in React?

Is there a way to properly setState when needing async/await data inside it? I know it's not recommended, but I'm struggling with getting data before setting the state. Any suggestions? codesanbox: https://codesandbox.io/s/infallible-mendeleev-6 ...

Ways to define global variables with JavaScript

What is the correct way to create a global variable in JavaScript that is available throughout the entire HTML document? Is it possible to access a variable declared in one script from another? ...

React Component: Issue with conditional "if else" statement not refreshing in return statement

Starting out in React and the front-end field with minimal experience. Currently attempting to dynamically change the color of the "fill" property in a polygon element using React. If the percentage is greater than 50, I want the color to be green; otherw ...

Unable to add the div using a jQuery click event

Hey there, I'm looking to add a div dynamically when clicking on another div. Check out the code snippet below: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8> <title></title> <script src= ...

Utilize the client-side JavaScript file with ejs framework

Recently, I have been working on creating a website using Express and EJS. I discovered that using just one JavaScript file for all my EJS (view) files was causing issues. If I target a DOM element in one view page and it doesn't exist in another, I w ...

I have tried to create two apps in AngularJS, but unfortunately, they are not functioning as expected

Having trouble with implementing 2 ng-app in a single html page. The second one is not working. Please review my code and point out where I am making a mistake. <div id="App1" ng-app="shoppingCart" ng-controller="ShoppingCartController"> &l ...

What is the most effective way to transfer an array from one PHP file to a different JavaScript file?

Utilizing AJAX to send a request to another php page and retrieve the result of a query, I originally used xmlhttprequest to pass the php logic along with the query information. However, I wanted to separate the presentation logic from the actual code logi ...

Error unfound: [CLIENT_MISSING_INTENTS]: The Client requires valid intents to function properly

I've gone through multiple tutorials, but I keep encountering an uncaught TypeError. Despite following the suggested solutions, the error persists. I even tried implementing the "intent" solution, but it's prompting a change in "const client = ne ...

Update the data in Firebase, but revert it back to the original state after a few seconds with the use of "angularFire."

I'm currently working on updating data in the Firebase Realtime Database using Angular and AngularFire. The issue I'm facing is that even though the data changes successfully, it reverts back to the original data after a few seconds. Below is the ...

What is the preferred approach for managing user data for logged in users - server-side or client-side?

I am developing a cutting-edge REST-based application using JWT authentication. One option is to retrieve all user posts with a server-side variable that accepts an ID parameter from the client's GET request: http://example.com/api/v1/posts?user_id=1 ...

Having identical functions with varying IDs and the same variable in Ajax is causing issues

Here, I have two HTML rows with different IDs. When the function ItemSearch() is called, I want to display the search results for both IDs. I am new to JavaScript and AJAX. ***1st Html*** <div class="justlauchSearch"> <div class="input-gro ...

Is Vue instance created if element is present?

I am currently developing an application where some pages contain Vue instances, while others do not. My issue arises when switching from one page to another, and the following warning message appears: [Vue warn]: Cannot find element: #element-id How can ...

Classic ASP offers a feature that allows users to select all checkboxes at once

I'm looking to create a functionality where there is a 'Select all' checkbox along with individual checkboxes for each database record. Is it possible to use JavaScript to ensure that when the 'Select all' checkbox is checked, all ...

Exploring the use of ASP:Label, <span>, and accessing elements by their ID

Attempting to access a control within a specific class has been challenging for me. HTML Code: <span class="GeoPanelHeaderLeft"> <asp:Literal ID="LiteralHeaderText" runat="server" Text="New Survey Ticket"></asp:Literal> & ...

Changing the value of an input field based on a user's input

Looking to automatically format any input in an input field as currency: <input type="text" value="0,00 EUR" onChange={(e) => { e.target.value = convertToCurrency(e.target.value) }} /> const convertToCu ...

What is the best way to import a JSON file in VueJs without the need for compilation?

I am facing an issue while developing a VueJs application. I have a JSON file containing an array of objects, and when displaying based on search criteria, it results in generating a very large app.js file, causing the application to boot slowly. Currentl ...

Refresh Twitter Bootstrap Tooltip after deactivating/removing

I have a quick question. I am currently dealing with data that is constantly changing and displayed from a selected item in a table. To monitor for overflow, I have implemented the following code: if (event.target.offsetWidth < event.target.scrollW ...