Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

dynamically swapping a method of a button in vuejs based off of data

I have a vuejs sample where I am trying to dynamically change the button based on the data found in the vue model? So for instance, if the data says video in the returned data, I want the method to trigger and alert. If the data says another, I want it to display another message.

new Vue({
  el: "#app",
  data: {
    click:'video'
  },
  methods: {
    step1: function(){
        alert("click1")
    },
        step2: function(){
        alert("click2")
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <h2>Checklist:</h2>
  <button v-on:click="step1()">
  Click 1
  </button> 
  
</div>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Try to use an inline handler with ternary operator :

 <button v-on:click="click==='video'?step1():step2()">
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading