Skip to content
Related Articles
Open in App
Not now

Related Articles

How to dynamically add or remove items from a list in Vue.js ?

Improve Article
Save Article
  • Difficulty Level : Expert
  • Last Updated : 19 Feb, 2021
Improve Article
Save Article

Vue is a progressive framework for building user interfaces. The core library is focused on the view layer only and is easy to pick up and integrate with other libraries. Vue is also perfectly capable of powering sophisticated Single-Page Applications in combination with modern tooling and supporting libraries. 

Using Vue.js one can easily add or remove items in a list dynamically using the v-model directive. This directive binds all the possibilities to a single channel. When the user selects an option from the list of available ones, it adds it to the list of values. Similarly, if the user deselects any option, it removes it from the list of values.

Example:

index.html




<html>
<head>
    <script src=
      </script>
</head>
<body>
    <div id='parent'>
      <h1 style="color: green;">
        GeeksforGeeks
      </h1>
          
          <p><strong>Favourite Sports:</strong>
          </p>
  
        <input type="checkbox" id="cricket" 
               value="cricket" v-model="sports">
        <label for="cricket">Cricket</label>
        <input type="checkbox" id="football" 
               value="football" v-model="sports">
        <label for="football">Football</label>
        <input type="checkbox" id="hockey" 
               value="hockey" v-model="sports">
        <label for="hockey">Hockey</label>
        <input type="checkbox" id="badminton"
               value="badminton" v-model="sports">
        <label for="badminton">Badminton</label>
        <input type="checkbox" id="arching" 
               value="arching" v-model="sports">
        <label for="arching">Arching</label>
          
        <p><strong>Sports You  Like:</strong>
          {{ sports }}
        </p>
  
    </div>
    <script src='app.js'></script>
</body>
</html>

app.js




const parent = new Vue({
    el : '#parent',
    data : {
        sports : []
    }
})

Output:


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!