In VueJS after append() element, click method doesn't work


Firmansyah

I can solve this problem in jquery using method, $(document).on('click', '.newButton', function(){});how to solve the same problem in vue

new Vue ({
  el: '#app',
  data: {
    oldButton: function () {
      $('#app').append('<button v-on:click="newButton">The New Button</button>');
    },
    
    newButton: function () {
      console.log('Hello World!');
    },
  },
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.min.js"></script>
<div id="app">
  <button v-on:click="oldButton">The Old Button</button>
</div>

Alireza Fattahi

If you want to have a series of buttons:

const app = new Vue({

  el: '#app',

  data: {
    btns: []
  },

  methods: {
    addBtn() {
      this.btns.push({
         name: 'Dynamic Button'
      })
    },
    showMsg(index) {
      console.log('Hello World!, from Button ' + index)
   }
  }

})

and:

<div id="app">    
  <ul>
     <li v-for="(btn, index) in btns">
        <button  @click="showMsg(index)" type="text"> {{ btn.name}}</button>             
     </li>
  </ul>    
<button @click="addBtn">Add Button</button>    
</div>

Related


In VueJS after append() element, click method doesn't work

Firmansyah I can solve this problem in jquery using method, $(document).on('click', '.newButton', function(){});how to solve the same problem in vue new Vue ({ el: '#app', data: { oldButton: function () { $('#app').append('<button v-on:click="new

In VueJS after append() element, click method doesn't work

Firmansyah I can solve this problem in jquery using method, $(document).on('click', '.newButton', function(){});how to solve the same problem in vue new Vue ({ el: '#app', data: { oldButton: function () { $('#app').append('<button v-on:click="new

In VueJS after append() element, click method doesn't work

Firmansyah I can solve this problem in jquery using method, $(document).on('click', '.newButton', function(){});how to solve the same problem in vue new Vue ({ el: '#app', data: { oldButton: function () { $('#app').append('<button v-on:click="new

jQuery: Append element method doesn't work

mxmtsk I have the following code where I am attaching the notification to the notification container. I want to fade in the notification after adding it, but it tells me that the function is undefined, but the jQuerys hidemethod works fine. const notification

click event doesn't work with new html with append even after

Adrian Hi I am creating a new element and children of that element in beforesendAjax . Later, if I click on the parent element of the child element, I want to show it. $('#submit-task').on('click', function() { // variable declarations etc jQuery.ajax(

$(element).remove && append(element) doesn't work

Ethan E10 I'm trying to preview a file from my filesystem, the preview works the first time I click, but it never updates after I click on a different element. I think there might be something wrong with my remove and append functions. Here is my JS code funct

jQuery click event doesn't work after adding method

Edward So I have this button, which adds a new row to the table, but my problem is that .new_participant_formafter the append method is executed , it no longer listens to the click event. http://jsfiddle.net/cTEFG/ Click Add New Entry, and then click the form

jQuery click event doesn't work after adding method

Edward So I have this button, which adds a new row to the table, but my problem is that .new_participant_formafter the append method is executed , it no longer listens to the click event. http://jsfiddle.net/cTEFG/ Click Add New Entry, and then click the form

jQuery click event doesn't work after adding method

Edward So I have this button, which adds a new row to the table, but my problem is that .new_participant_formafter the append method is executed , it no longer listens to the click event. http://jsfiddle.net/cTEFG/ Click Add New Entry, and then click the form

.click() method doesn't work

Summer Sheikh Here I have a link with a category name "lineIcon" and for this I have written 2 functions, the first one is as follows: - I have added a category "overlayShow". Now, click on the "overlayshow" iam, add another class called "overlayHide" and dele

Method click doesn't work

Pheromones When the button is clicked, a dataTable will be populated from the database. I want to jump to the div containing that dataTable programmatically on click of a button. Here's what I've tried (but it didn't work for me): ... <tr> <td colspan="2"

.click() method doesn't work

Summer Sheikh Here I have a link with a category name "lineIcon" and for this I have written 2 functions, the first one is as follows: - I have added a category "overlayShow". Now, click on the "overlayshow" iam, add another class called "overlayHide" and dele

jQuery click doesn't work with append

wizard $('.spinner').on('click', '.btn:first-of-type', function() { var $input = $(this).closest('.numeric-btn').find('.spinner input'); $input.val( parseInt($input.val(), 10) + 1).keyup(); }); when i replace the class divwhere.spinner $('.side-form > div

jQuery click doesn't work with append

wizard $('.spinner').on('click', '.btn:first-of-type', function() { var $input = $(this).closest('.numeric-btn').find('.spinner input'); $input.val( parseInt($input.val(), 10) + 1).keyup(); }); when i replace the class divwhere.spinner $('.side-form > div

VueJs 2 click doesn't seem to work

Dylan Glockler I'm not getting any errors and it's compiling so I'm not sure what I'm doing wrong.I searched for this topic without success. I have BulkExpenses.vue which pulls and displays some expense records, then BulkExpenses.vue is a nested component that

VueJs 2 click doesn't seem to work

Dylan Glockler I'm not getting any errors and it's compiling so I'm not sure what I'm doing wrong. I searched for this topic without success. I have BulkExpenses.vue which pulls and displays some expense records, then BulkExpenses.vue is a nested component tha

VueJs 2 click doesn't seem to work

Dylan Glockler I'm not getting any errors and it's compiling so I'm not sure what I'm doing wrong. I searched for this topic without success. I have BulkExpenses.vue which pulls and displays some expense records, then BulkExpenses.vue is a nested component tha

Append() doesn't work on JTextArea element

Halep: I want to create a graphical console which will provide the user with information about the current state of the program. I have planned to use it for JTextArea, but there is a problem with the append() method. I still have empty JTextArea even after us

Jquery event doesn't work on element in append()

Sag Currently, I'm creating something that just allows me to create tasks, click on them when done and swipe left to get rid of them. The problem I'm having is that when new tasks are attached, they are not affected by any JQuery events in the script. Tl;dr: T

Append() doesn't work on JTextArea element

Halep: I want to create a graphical console which will provide the user with information about the current state of the program. I have planned to use it for JTextArea, but there is a problem with the append() method. I still have empty JTextArea even after us

Append() doesn't work on JTextArea element

Halep: I want to create a graphical console which will provide the user with information about the current state of the program. I have planned to use it for JTextArea, but there is a problem with the append() method. I still have empty JTextArea even after us

Append() doesn't work on JTextArea element

Halep: I want to create a graphical console which will provide the user with information about the current state of the program. I have planned to use it for JTextArea, but there is a problem with the append() method. I still have empty JTextArea even after us

on('click') doesn't work after off('click')

username The following code doesn't work. It removes the disabled class, but the div is not clickable again. I mean the function can only run once. $(document).ready(function(){ $('div.downloadbutton').on('click', function(e) { $('div.downloadbutton'

on('click') doesn't work after off('click')

username The following code doesn't work. It removes the disabled class, but the div is not clickable again. I mean the function can only run once. $(document).ready(function(){ $('div.downloadbutton').on('click', function(e) { $('div.downloadbutton'