Vue.js Text Field with Clearable Button
Dynamic Input Field with Clear Button in Vue.js: Vue.js code creates an interactive input field with a clearable button. The input field has a bottom border line and changes its background color when it's clicked. The clear button only appears when the input field has a value and is hidden when the input field is not focused. This code is fully customizable and can be used in any Vue.js application to provide a clearable input field with a stylish look and feel

written

reviewed

updated

Thanks for your feedback!
Your contributions will help us to improve service.
What is the code for a Vue.Js text field with a clearable button and how does it work
HTML
- The code has a div with id
app
- Within the div, there is a header tag with an empty string and a div with class
input-group
- The div with class
input-group
contains an input and a button element
JavaScript
- A new Vue instance is created using
const app = new Vue({...})
- The Vue instance is bound to the div with id
app
usingel: "#app"
- The Vue instance has data properties
value
andisActive
which are both empty strings and a boolean false respectively - The Vue instance has a method
clearValue
which setsvalue
to an empty string
CSS
- The class
input-group
setsdisplay: flex
andalign-items: center
- The styles for the
input
element sets the border, padding, and outline properties - The styles for the class
input.has-value
sets the bottom border color to blue - The styles for the class
input.active
sets the background color to light gray - The styles for the
button
element sets the background, border, padding, cursor, and border-radius properties
Customizable Clearable Input Field Component in Vue.js"
Copied to Clipboard
23
<div id="app">
<div class="input-group">
<input type="text" v-model="value" :class="{ 'has-value': value, 'active': isActive }"
@click="isActive = true" @blur="isActive = false" />
<button v-if="value" @click="clearValue">Clear</button>
</div>
</div>
<script type="module">
const app = new Vue({
el: "#app",
data() {
return {
value: '',
isActive: false
}
},
methods: {
clearValue() {
this.value = '';
}
}
});
</script>
Output of above example
Ad