Avoid triggering the parent modal to close when a child element is clicked in VueJS

In my Vue application, there is a situation where I have a button called "detach button" with a @click function inside an element that is clickable. When clicking on this parent element, it triggers another @click function and toggles a Bootstrap modal event.

The issue arises when I click on the child button, as it ends up triggering both events (the @click and the Modal toggle) of the parent element.

I have attempted to use @click.stop and @click.prevent on the child button to prevent this behavior, but it still triggers the events of the parent. Is there a way to avoid triggering the parent logic in this scenario?

Any help or suggestions would be greatly appreciated!

<tbody>
    <tr
      v-for="project in $store.state.projectlist"
      :key="project.id"
      class="custompointer"
      @click.stop="storeCurrentProfileProjectBinding(project)"
      data-bs-toggle="modal"
      data-bs-target="#editProjectModal"
    >
      <td>
        <div class="symbol symbol-40px">
          <img
            :src="project.clientlogo"
            class="h-50 align-self-center"
            alt=""
          />
        </div>
      </td>
      <td>

        <div class="d-flex flex-row">
          <a
            href="#"
            class="text-dark fw-bold text-hover-primary fs-6"
          >{{ project.clientname }}</a>
          <div class="ms-1">
            <!--begin:detach button-->
            <a
              href="javascript:void(0)"
              class="text-hover-danger"
              @click.stop="detachFromThisProject(project)"
            >
            </a>
            <!--end:detach button-->
          </div>
        </div>

      </td>


    </tr>

  </tbody>

Answer №1

Make sure to add some content or specify a size for your link:

new Vue({
  el: "#demo",
  data() {
    return {
      items: [{id: 1, clientname: 'a', clientlogo: 'https://picsum.photos/40'}, {id: 2, clientname: 'b', clientlogo: 'https://picsum.photos/42'}]
    }
  },
  methods: {
    detachFromThisProject(p) {
      console.log('child')
    },
    storeCurrentProfileProjectBinding(p) {
      console.log('parent')
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo">
  <table>
    <tbody>
    <tr
      v-for="project in items"
      :key="project.id"
      class="custompointer"
      @click="storeCurrentProfileProjectBinding(project)"
      data-bs-toggle="modal"
      data-bs-target="#editProjectModal"
    >
      <td>
        <div class="symbol symbol-40px">
          <img
            :src="project.clientlogo"
            class="h-50 align-self-center"
            alt=""
          />
        </div>
      </td>
      <td>
        <div class="d-flex flex-row">
          <a
            href="#"
            class="text-dark fw-bold text-hover-primary fs-6"
          >{{ project.clientname }}</a>
          <div class="ms-1">
            <!--begin:detach button-->
            <a
              href="javascript:void(0)"
              class="text-hover-danger"
              @click.stop="detachFromThisProject(project)"
            >detach
            </a>
            <!--end:detach button-->
          </div>
        </div>
      </td>
    </tr>
  </tbody>
  </table>
</div>

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

Several middlewares using router.params()

Is it possible to include multiple middlewares as parameters in the function router.params() in Node-Express? I currently have the following setup: const checkAuth = (req, res, next) => {console.log("checking auth"); next()} const checkAuth = ...

Manipulate the value(s) of a multi-select form field

How can you effectively manage multiple selections in a form field like the one below and manipulate the selected options? <select class="items" multiple="multiple" size="5"> <option value="apple">apple</option> <option va ...

What is the importance of having the http module installed for our Node.js application to function properly?

After exploring numerous sources, I stumbled upon this code snippet in the first application: var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); r ...

Indicate the node middleware without relying on the port number

My website is hosted at mywebsite.com and I am using node and express for middleware services, pointing to mysite.com:3011/api. The website is hosted statically on Ubuntu 16 (Linux) and the middleware is run separately using pm2 (node server). I want to b ...

I am currently working on a project using ar.js

I came across a glitch code that triggers a video when scanning the marker. However, I encountered an issue where it only works on desktop. On Chrome for Android, the video does not appear, only the sound can be heard. I need assistance as my coding knowle ...

Looking for a solution to organize the dynamically generated list items in an HTML page

I am currently working on a movie listing website where all the movies are displayed in sequence based on their #TITLE#. The webpage is generated automatically by the software using a template file. Here is the section of code in the template file that sho ...

Difficulty with rendering subcomponents of child components in Vue 3

For my web application, I utilized Vue 3.0.5 along with Webpack 5.21.2 and Webpack CLI 4.5.0. One of the key elements is a global component named my-component.js: import SecondComponent from './second-component.vue'; app.component('global-co ...

Is it possible to automatically close the modal by clicking outside of it

How can I make sure that my modal box only closes when clicking outside of it, and not when clicking on the buttons inside? I have a ref to the parent element that successfully closes the modal on click outside, but currently it also closes if I click on t ...

Adding methods to a constructor's prototype in JavaScript without explicitly declaring them

In the book Crockford's JavaScript: The Good Parts, there is a code snippet that demonstrates how to enhance the Function prototype: Function.prototype.method = function (name, func) { this.prototype[name] = func; return this; }; Crockford elabo ...

How can one effectively develop a component library that is compatible with both React and Preact?

I'm currently in the midst of a project that involves both React and Preact. I've come across a situation where I need to utilize the same component for both React and Preact. Would it be wise to package the component into an npm library? What a ...

What could be the reason behind the validation error occurring in this Laravel 8 and Vue 3 application?

Currently, I am developing a registration form with a Laravel 8 API and integrating it with a Vue 3 front-end. In the AuthController, I have implemented the following code snippet for user registration: ... On the front-end side, my setup looks like this ...

Exploring the contrast between window and document within jQuery

I'm curious about the distinction between document and window in jQuery. These two are commonly utilized, but I haven't quite grasped their differences. ...

Show a loading icon as the synchronous Ajax request is being sent

Seeking a way to show a spinner while making a synchronous Ajax request to fetch data from the server. Acknowledging that asynchronous mode is preferred, but not permitted in this case. Aware that a sync ajax request can cause browser blocking. An attemp ...

Required environment variables are absent in the application form

Currently, I am working on developing a Next.js application and implementing CRUD functionality. However, I encountered an error while creating the "Create" page, which seems to be related to environment detection. Just to provide some context, I am using ...

"Angularjs feature where a select option is left blank as a placeholder, pointing users

Currently, I am working with AngularJS (version < 1.4). When using ng-repeat in select-option, I encounter an extra blank option which is typical in AngularJS. However, selecting this blank option automatically picks the next available option. In my sce ...

Collecting information from form submissions, accessing data from the database, and displaying the results

I am currently working on a project using nodejs, express, and mongodb within the cloud9 IDE. My goal is to create a page with a form that allows users to input data, search for corresponding information in the database, and display that data on the same ...

Adding external stylesheets or libraries in Express is a simple process that can greatly

Currently, I am in the process of developing a todo application using the express framework on a node server. The starting point for my application is an index.ejs file located in the same folder as my jquery.min.js file. However, when I attempt to include ...

How do I redirect with a GET method after calling the *delete* method in Node / Express server?

As someone new to AJAX and Node, I encountered a dilemma that I hope to get some guidance on. Here's the issue: I have a DELETE ajax call that removes a row from the database and I want to redirect back to the same route with a GET method afterwards. ...

Do not use the .map method! Caution: Every child component in a list must be assigned a unique "key" prop

I have recently started working with NEXT JS and I am encountering a peculiar issue for which I haven't been able to find a solution online. The warning message I'm getting is: "Each child in a list should have a unique key prop." Warning: Each c ...

Issue regarding ports: deployment of a project using vue.js and node.js on Heroku encountered errors

Having issues deploying my fullstack project. It builds and runs smoothly locally, but on Heroku, I encounter a 503 Service Unavailable error after running the 'git push heroku master' command and the subsequent automatic build process. This coul ...