How to make input read only based on particular value in Vue?

66,511

Solution 1

Please note that, according to HTML specs, the select tag in HTML doesn't have a readonly attribute.

However, in general case, I'd go with something like this:

<input class="form-control" id="selectCategory" :readonly="cat_id >= 1">

Basically, the documentation says that if an attribute value evaluates to false, then the attribute being omitted. See here for further details.

Solution 2

You can do something like this:

<input v-bind:readonly="isReadOnly">

Solution 3

I ran into this issue while making a form for a password changing form and I encountered the browser trying to autocomplete everything. This should prevent the autocomplete from firing and also provide a method to change the value to suit your needs.
For some reason adding a v-bind:readonly property of true reverts to readonly="readonly"
Here is my currently working example which removes and re-adds the readonly property on focusout.

Vue Template HTML

<input type="password" v-model="user.password" placeholder="Password" @focusin="toggleReadonly" @focusout="toggleReadonly" :readonly="true">

Method

toggleReadonly(event){
          event.preventDefault()
          if(event.target.getAttribute('readonly') == 'readonly'){
              event.target.removeAttribute('readonly')
          }
          else{
              event.target.setAttribute('readonly', 'readonly')
         }
 }

Solution 4

you could have a computed property that returns a boolean dependent on whatever criteria you need.

<input type="text" :disabled=isDisabled>

then put your logic in a computed property...

computed: {
 isDisabled() {
// evaluate whatever you need to determine disabled here...
   return true;
 }
}

JSFIDDLE https://jsfiddle.net/sureshamk/0dzvcf4d/1320/

Share:
66,511

Related videos on Youtube

Neha
Author by

Neha

I'm a JavaScript Developer, working with technologies like HTML5, CSS3, Javascript(ES6 and beyond), jQuery, vuejs, Extjs, sencha and core java. Currently, I'm part of the development team at infoway, where we make the web applications , Single page applications and multi page applications. Previously, I worked at SDP LAbs, India based company, specialized in web solutions for the Project Management Software(Proofhub) which is a SPA in EXTjs.

Updated on June 01, 2020

Comments

  • Neha
    Neha about 4 years

    How to make an input field read only based on Vue data?

    For example:

    <select class="form-control" 
            id="selectCategory" 
            :disabled="cat_id >= 
                1" 
            name="cat_id">
    

    I want to make the field read only but not disabled. How can I achieve this?

  • José Manuel Blasco
    José Manuel Blasco over 5 years
    Disabled is not the same as readonly. A disabled field in a form won't be submitted while a readonly will be. Further, CSS styles are not the same for readonly and disabled.
  • Neha
    Neha about 5 years
    autocomplete="off" for preventing an input to autocomplete
  • Neha
    Neha about 5 years
    <input type="password" v-model="user.password" placeholder="Password" autocomplete="off">