Disable buttons in VueJS by binding :disabled="isDisabled" and updating the ref value with an event handler inside the Composition API setup.
Scalable, Secure, and High-Performance Solutions for Your Business.
In Vue.js, controlling the disabled state of a button is a common UI task especially for forms, validations, or async operations. Vue makes this easy using the :disabled directive, which binds a Boolean condition to the button’s disabled attribute.
You can bind the disabled attribute to a ref or data property.
Example – Toggle Button Enabled State
<!– ToggleButton.vue –><script setup>import { ref } from ‘vue’;
const isDisabled = ref(true);
function toggleButton() {
isDisabled.value = !isDisabled.value;
}
</script>
<template>
<button :disabled=”isDisabled”>Click Me</button>
<br />
<button @click=”toggleButton”>
Toggle Disabled State
</button>
</template>
Use a computed property to determine if a form is valid, and disable the submit button accordingly.
Example – Basic Validation
<!– FormValidation.vue –><script setup>
import { ref, computed } from ‘vue’;
const name = ref(”);
const email = ref(”);
const isFormValid = computed(() => {
return name.value.trim() !== ” && email.value.includes(‘@’);
});
</script>
<template>
<input v-model=”name” placeholder=”Name” />
<input v-model=”email” placeholder=”Email” />
<button :disabled=”!isFormValid”>Submit</button>
</template>
Useful when submitting forms, uploading files, or calling APIs.
Example – Simulate API Call
<!– AsyncButton.vue –><script setup>
import { ref } from ‘vue’;
const loading = ref(false);
function handleClick() {
loading.value = true;
// Simulate async operation
setTimeout(() => {
alert(‘Done!’);
loading.value = false;
}, 2000);
}
</script>
<template>
<button :disabled=”loading” @click=”handleClick”>
{{ loading ? ‘Processing…’ : ‘Submit’ }}
</button>
</template>
Also Read: The Vue JS Best Practices In Detail
To disable buttons in Vue.js, bind the disabled attribute to a reactive variable or computed condition using :disabled. This ensures your UI reflects the latest logic based on user input, loading states, or validation rules.
eSparkBiz is rated 4.9 Stars
Real People, Real Stories
See why 300+ startups & enterprises trust eSparkBiz with their software outsourcingIn Vue 2, you need to explicitly install plugins using Vue.use() to add to Vue’s capabilities. This method installs the plugin globally so all components…
In Vue 3, the best way to define global variables is by adding them to app.config.globalProperties during app setup. This makes them accessible across the…
In Vue 3's Composition API, watching for prop changes is essential when you need to respond to updated values passed from a parent component. Unlike…
Let’s discuss how our dedicated experts can help.