Skip to content
Advertisement

Vue.js dynamically appending HTML when clicking on a button

So basically what I am trying to achieve is allowing the user to be able to add to an existing set of information that will be passed on to Vue from JSON.

So the following code is my HTML code which comprises of the div element with the id objectiveArea that vue will render to and a hard coded button for the users to add more entries.

JavaScript

And here is my Javascript where I pass the JSON into vue for it to render as well as having an onclick event when the add button is triggered. The function addNewObjectiveRow serves as a template to append when the user clicks add and the objectiveElements is a template for vue to render the existing JSON to the HTML.

JavaScript

However, the above code renders the existing information from the JSON just fine in the HTML but when I click on the add button, nothing happens at all. Am I missing something or getting it all wrong?

Advertisement

Answer

https://v2.vuejs.org/v2/guide/list.html

HTML

JavaScript

JS

JavaScript

Clicking the button will execute addItem() method which will add a new item into data items and it will automatically render new li

Advertisement