Utilizing vue.js 3 to implement dual @click events on a single HTML element

I need to assign 2 mouse click events to a div element. One should be triggered by holding down the shift key while clicking, and the other by a regular click, like this:

<div @click.shift="aaa()" @click="bbb()">...</div>

The issue I'm facing is that when I shift+click on the div, both aaa() and bbb() functions are executed. I have tried using modifiers like @click.shift.stop but it didn't prevent bbb() from being called every time I clicked on the div. Is there a way to resolve this without combining the two types of click events into a single function, for example ccc(), which would then call either aaa() or bbb()?

Answer №1

The key you need to use is the "exact" modifier.

<div @click.shift="aaa()" @click.exact="bbb()">...</div>

https://vuejs.org/guide/essentials/event-handling.html#exact-modifier

The .exact modifier allows for precise control over the specific combination of system modifiers required to trigger an event.

Therefore, this click event will only be triggered if no modifier is used, preventing it from executing when the @click.shift is activated. Keep in mind that this also blocks alt+click, ctrl+click, etc.

Answer №2

To achieve this, you can create a single click handler that will dispatch the event as needed.

new Vue({
  el: '#app',
  methods: {
    handleClick() {
      console.log('Click event handled!');
    },
    handleEvent(event) {
      if (!event.shiftKey) {
        console.log('Handle event executed!');
      } else {
        this.handleClick();
      }
    }
  }
})
div {
  background-color: green;
  padding: 20px;
}
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>

<div id="app">
  <div @click="handleEvent($event)">Click me!</div>
</div>

Answer №3

new Vue({
  el: '#app',
  methods: {
    aaa() {
      console.log('aaa() executed!');
    },
    bbb(event) {
      if (!event.shiftKey) {
        console.log('bbb() executed!');
      }
    }
  }
})
      div {

        background-color: green;
        padding: 20px;

      }
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>

    <div id="app">
      <div @click.shift="aaa()" @click="bbb($event)">Click me!</div>
    </div>

I trust this solution aids you!

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

Instantly display selected image

I have encountered some challenges with my previous question on Stack Overflow as I couldn't find a suitable solution. Therefore, I decided to explore an alternative method for uploading images. My current goal is to upload an image immediately after ...

Using Firebase with Vue.js

I'm currently working on developing a straightforward read/write application using Vue.js, VueFire, and Firebase. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>App</title> <!-- Vue --> ...

Executing unique calculations on Kendo UI Grid Columns

For experienced users, this may seem simple, but as a newcomer, I'm struggling with a basic arithmetic task. I want to multiply one of the column values (DealValue) by 0.05 in my Kendo grid setup. Despite looking through the Kendo docs, I couldn' ...

The issue of slides overlapping in a Javascript slideshow during auto play mode

I encountered an issue while creating an automatic slideshow with 4 slides. Upon autoplay for the first time, slide 1 overlaps slide 4, as well as slide 2 and 3. This only happens once after the site loads. However, on subsequent playthroughs or when using ...

Achieving a sticky scrollbar effect in CSS/JavaScript for scrolling within nested divs

I am trying to create a table with vertical scrolling for the entire table content. Additionally, I need two columns within the table to be scrollable horizontally, but the horizontal scrollbars disappear when the vertical scrollbar is present. How can I k ...

Refining to Showcase One Menu Selection

I am having an issue with my bootstrap menu that includes a search field. The problem is that when I try to filter the dropdown menu using the search field, it filters all dropdown items instead of just the one I want. For example, if I search for a term f ...

The tension settings in Chart.JS appear unusual

I recently updated to ChartJS v4.0.1 and noticed a new option called tension for curving the line chart. However, I'm not satisfied with how it looks. The tension option ranges from 0 to 1, and I've experimented with different values to enhance ...

Innovative application transforms rigid input with dynamic hyphens

I am currently working on an input field specifically designed for Social Security Numbers (SSNs). My aim is to have an input with pre-placed hyphens, allowing the user to simply enter their 9-digit SSN while the numbers automatically space themselves arou ...

Incorporating external libraries is seamless when used in a .html document, however, they may encounter limitations when integrated

I am facing an issue while migrating my code from an .html file to a Quasar project. The code runs perfectly fine in the .html file, but I encounter errors when implementing it in Quasar. Here is the original code from the index.html file: <!DOCTYPE ht ...

How can I effectively display personalized information from an MSAccess database on a WordPress website?

I am seeking advice from experienced Wordpress developers. My organization possesses an internal MS Access database that comprises various tables, reports, and input forms. The structure of the database is relatively straightforward, encompassing informati ...

"Learn the technique of displaying or concealing a row in a ListView with the help of

I'm currently utilizing an Asp.net Listview for displaying data in a "grid" format. Below is the code snippet: <asp:ListView ID="lvDmr" runat="server" DataSourceID="dsDmr" DataKeyNames="id"> <ItemTemplate> &l ...

Encountering an npm Error while setting up a new Vue project with Vue-CLI

I'm a new Vue user and have been searching everywhere for the right solution but haven't had any luck yet. Can someone please help me with this issue? I'm having trouble creating a new Vue project. Initially, I tried running npm install -g ...

Modifying Stroke color in HTML5 Canvas

I am attempting to create a unique visual effect by drawing a circle resembling a clock using canvas 2d context. The idea is to initially draw the circle in black starting at point p1, and then when I complete a full circle tour it should erase as the colo ...

Tips for consolidating all functions into a single file for ReactJS inheritance:

I'm curious about something. In Angular JS, we have the ability to create a global service file that can be inherited by every component. This allows us to use the functions written in the global service file within each respective component. Is ther ...

How do you incorporate ScrollTop animations using @angular/animations?

I'm attempting to recreate the animation showcased on Material.io: https://i.stack.imgur.com/OUTdL.gif It's relatively easy to animate just the height when clicking on the first card in the example above. The challenge arises when you click on ...

Creating personalized columns in a BootstrapVue table

I'm having an issue with the b-table component in bootstrap-vue. The array containing my items is structured like this: [{ id: 1, name: "Test", phone: 555-111-666, address: "Some Address", //etc... }] I have a couple of question ...

Including input within a component causes the input to capture all click events

I've been working on a project where I need to create a color picker component. When you click on the .swatch element, a popover opens up with a .cover div that covers the whole screen. Clicking on the cover closes the popover, following standard beha ...

Reload entire page for AJAX request if JavaScript is not enabled

Currently utilizing JSF2. I have a button that triggers an action and then updates a section of the page (fairly standard). <h:commandButton value="foo" action="#{myBean.myAction}" > <f:ajax execute="@form" render="#content" /> ...

Accessing information from a database table using Highchart and Ruby on Rails (ROR

I have a Ruby on Rails application that utilizes highcharts. I am currently working on enhancing it to display the amount of time spent on specific projects. To demonstrate what I am trying to achieve, I have created a JSFiddle example which can be found h ...

Input specific ng-if conditions

I'm a bit confused about the conditions for my ng-if and could use some assistance. I have a form on my page that is rendered using ng-repeat and a couple of custom filters. You can check out this plunker. The issue I'm facing is that I need to p ...