162

Question:

How to call multiple functions in a single @click? (aka v-on:click)?

I tried

  • Split functions with a semicolon: <div @click="fn1('foo');fn2('bar')"> </div>;

  • Use several @click: <div @click="fn1('foo')" @click="fn2('bar')"> </div>;

but how to do it properly?

P.S.: For sure I always can do

<div v-on:click="fn3('foo', 'bar')"> </div>

function fn3 (args) { 
  fn1(args);
  fn2(args);
}

But sometimes this isn't nice.

Penny Liu
  • 11,885
  • 5
  • 66
  • 81
Sergei Panfilov
  • 15,004
  • 15
  • 70
  • 94

19 Answers19

357

On Vue 2.3 and above you can do this:

<div v-on:click="firstFunction(); secondFunction();"></div>
// or
<div @click="firstFunction(); secondFunction();"></div>
Sergei Panfilov
  • 15,004
  • 15
  • 70
  • 94
Stuart Cusack
  • 3,686
  • 2
  • 11
  • 3
  • are the methods guaranteed to execute in order? – Andrew Brown Oct 18 '21 at 02:50
  • This produces a warning on Vue.js v2.6.14: ```unreachable code after return statement``` If that bothers anyone - you can always try something like this: ```
    {firstFunction(); secondFunction();}">
    ```
    – Glenn Carver Apr 08 '22 at 10:04
  • Not recommended for those who have `prettier` with `"semi": false` because will remove the semicolon. I recommend the plus sign between the name of the functions. – Arnon Jun 01 '22 at 17:01
51

First of all you can use the short notation @click instead of v-on:click for readability purposes.

Second You can use a click event handler that calls other functions/methods as @Tushar mentioned in his comment above, so you end up with something like this :

<div id="app">
   <div @click="handler('foo','bar')">
       Hi, click me!
   </div>
</div>

<!-- link to vue.js !--> 
<script src="vue.js"></script>

<script>
   (function(){
        var vm = new Vue({
            el:'#app',
            methods:{
                method1:function(arg){
                    console.log('method1: ',arg);
                },
                method2:function(arg){
                    console.log('method2: ',arg);
                },
                handler:function(arg1,arg2){
                    this.method1(arg1);
                    this.method2(arg2);
                }
            }
        })
    }()); 
</script>
ismnoiet
  • 4,053
  • 21
  • 30
33

If you want something a little bit more readable, you can try this:

<button @click="[click1($event), click2($event)]">
  Multiple
</button>

To me, this solution feels more Vue-like hope you enjoy

Shailen Naidoo
  • 347
  • 3
  • 9
  • also can do @click="click1($event); click2($event)". Here the main point is adding explicit event argument to the handler function. This way each method will get the same payload or the original event data. Important for the cases when you listen to custom event from Child component – Saulius Vikerta May 11 '21 at 20:11
14

to add an anomymous function to do that may be an alternative:

<div v-on:click="return function() { fn1('foo');fn2('bar'); }()"> </div> 
Wolfgang
  • 846
  • 5
  • 13
  • 14
    An ES6 alternative: @click="() => { function1(parameters); function2(parameters); }" – AdamJB Jun 28 '17 at 15:06
  • 1
    Important note: if one of your nested functions need `$event` as an arg, you'll need to pass it as a parameter to your new function to be used inside of it. Eg. `($event) => {func1($event); func2('x');}` – zcoop98 Aug 20 '20 at 21:00
11

Separate into pieces.

Inline:

<div @click="f1() + f2()"></div> 

OR: Through a composite function:

<div @click="f3()"></div> 

<script>
var app = new Vue({
  // ...
  methods: {
    f3: function() { f1() + f2(); }
    f1: function() {},
    f2: function() {}
  }
})
</script>
Inanc Gumus
  • 21,395
  • 9
  • 79
  • 93
9

This simple way to do v-on:click="firstFunction(); secondFunction();"

Mr Nobody
  • 377
  • 3
  • 4
8

This works for me when you need to open another dialog box by clicking a button inside a dialogue box and also close this one. Pass the values as params with a comma separator.

<v-btn absolute fab small slot="activator" top right color="primary" @click="(addTime = true),(ticketExpenseList = false)"><v-icon>add</v-icon></v-btn>
foundling
  • 1,536
  • 1
  • 14
  • 22
user2841183
  • 107
  • 1
  • 5
8

updated dec-2021

you need to separate with a comma like this:

<button @click="open(), onConnect()">Connect Wallet</button>
titleLogin
  • 549
  • 1
  • 8
6

in Vue 2.5.1 for button works

 <button @click="firstFunction(); secondFunction();">Ok</button>
Zhurov Konstantin
  • 682
  • 1
  • 8
  • 14
4

The Vue event handling only allows for single function calls. If you need to do multiple ones you can either do a wrapper that includes both:

<div @click="handler"></div>
////////////////////////////
handler: function() { //Syntax assuming its in the 'methods' option of Vue instance
    fn1('foo');
    fn2('bar');
}

EDIT

Another option is to edit the first handler to have a callback and pass the second in.

<div @click="fn1('foo', fn2)"></div>
////////////////////////////////////
fn1: function(value, callback) {
    console.log(value);
    callback('bar');
},
fn2: function(value) {
    console.log(value);
}
m_callens
  • 5,520
  • 6
  • 29
  • 50
3

Html:

<div id="example">
  <button v-on:click="multiple">Multiple</button>
</div>

JS:

var vm = new Vue({
  el: '#example',
  data: {
    name: 'Vue.js'
  },
  // define methods under the `methods` object
  methods: {
    multiple: function (event) {
      this.first()
      this.second()
    }
    first:  function (event) {
      //yourstuff
    }
    second: function (event) {
      //yourstuff
    }
  }
})

vm.multiple()
RaV
  • 1,019
  • 8
  • 23
3

I just want to add one small missing bit here which I felt missing in all of the answers above; that is you actually need to call the method rather than just passing its name as callable, when want to add multiple click handlers.

This might come as a surprise since Vue allows passing a callable to the click handler.

This works

<div><button @click="foo(); bar();">Button1</button></div>
<div><button @click="foo">Button2</button></div>

This does not

<div><button @click="foo; bar;">Button3</button></div>

JsFiddle example

Rohit
  • 2,887
  • 1
  • 19
  • 45
1

I'd add, that you can also use this to call multiple emits or methods or both together by separating with ; semicolon

  @click="method1(); $emit('emit1'); $emit('emit2');"
jewcub
  • 11
  • 1
1

You can use this:

<div @click="f1(), f2()"></div> 
Hosein
  • 305
  • 2
  • 7
1

You can do it like

<button v-on:click="Function1(); Function2();"></button>

OR

<button @click="Function1(); Function2();"></button>
1

Simply do like below:

  • with $event:

    <div @click="function1($event, param1); function2($event,param1);"></div>
    
  • without $event:

    <div @click="function1(param1); function2(param1);"></div>
    
Arash Younesi
  • 1,373
  • 1
  • 10
  • 21
1

Based on ES6 with anonymous functions:

<button @click="() => { function1(); function2(); }"></button>
runDOSrun
  • 9,345
  • 5
  • 41
  • 53
0

I was also looking this solution and used different methods and I found this one best for me. Just shared with you ***You can use template literals to use multiple function in one event in vuejs

<div @click="`${firstFunction() ${secondFunction() ${thirdFucntion()}`"></div>

Note:I am using vue3.

  • 1
    Are you sure this doesn't actually execute the three function when the component is rendered (rather than when this `div` is clicked)? – Tom Mar 20 '22 at 20:12
-2

you can, however, do something like this :

<div onclick="return function()
              {console.log('yaay, another onclick event!')}()" 
              @click="defaultFunction"></div>

yes, by using native onclick html event.

Ardhi
  • 2,535
  • 20
  • 28