<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<h3>Vue Add Remove Input Field</h3>
<div v-for="(field, index) in fields" :key="index" class="field-wrapper">
<input type="text" v-model="fields[index]" />
<button @click="removeField(index)">Remove</button>
</div>
<button @click="addField()" class="add-button">Add Field</button>
</div>
<script type="module">
const app = new Vue({
el: "#app",
data() {
return {
fields: [''] // initial value
};
},
methods: {
addField() {
this.fields.push(''); // add empty value to the fields array
},
removeField(index) {
this.fields.splice(index, 1); // remove field at the specified index
}
}
});
</script>
<style scoped>
/* Style the container */
#app {
display: flex;
flex-direction: column;
align-items: center;
padding: 20px;
box-shadow: 0px 2px 5px rgba(0, 0, 0, 0.3);
width: 400px;
margin: 0 auto;
}
/* Style the field wrapper */
.field-wrapper {
display: flex;
align-items: center;
margin-bottom: 10px;
}
/* Style the input field */
input[type="text"] {
padding: 10px;
border: none;
border-radius: 5px;
box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
margin-right: 10px;
font-size: 16px;
flex: 1;
}
/* Style the remove button */
button {
background-color: #f44336;
color: white;
border: none;
border-radius: 5px;
padding: 10px;
cursor: pointer;
font-size: 16px;
transition: background-color 0.3s ease;
}
button:hover {
background-color: #e53935;
}
/* Style the add button */
.add-button {
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
padding: 10px;
cursor: pointer;
font-size: 16px;
margin-top: 10px;
transition: background-color 0.3s ease;
}
.add-button:hover {
background-color: #3e8e41;
}
</style>
</body>
</html>