Categories
Vue Answers

How to Disable Input Conditionally in Vue.js?

Spread the love

Sometimes, we want to disable input conditionally in Vue.js.

In this article, we’ll look at how to disable input conditionally in Vue.js.

Disable Input Conditionally in Vue.js

We can disable input conditionally in Vue.js by passing in an expression into the disabled prop.

For instance, we can write:

<template>
  <div id="app">
    <button @click="disabled = !disabled">Toggle Enable</button>
    <input type="text" :disabled="disabled" />
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      disabled: false,
    };
  },
};
</script>

We have the data method which returns the disabled reactive property.

Next, we add a button that toggles the disabled reactive property between true and false when we click it.

Below that, we pass in the disabled reactive property as the value of the disabled reactive prop.

Now when we click the button, the input will become disabled and enabled.

Conclusion

We can disable input conditionally in Vue.js by passing in an expression into the disabled prop.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *