javascriptvue.jsinputbinding

How do I use one input to control the other in vuejs


I need to find a way to basically show "per Month" in the assessments_period input if the assessments fee input has a typed-in value.

I've tried bindings and components but can't pull this off.

            <div class="col-lg-6">
              <div class="form-group">
                <label>Assessment Fee <span class="note">C</span></label>
                <input type="text" class="form-control" v-model="document.assessments_fee" placeholder="$">
              </div>
            </div>
            <div class="col-lg-6">
              <div class="form-group">
                <label>Assessment Period <span class="note">C</span></label>
                <input type="text" class="form-control" v-model="document.assessments_period" placeholder="(per month/quarter/etc.)">
              </div>
            </div>



  data() {
    return {
      now: new Date().toISOString(),
      document: {
        assessments_fee: '',
        assessments_period: '',
      }
    }
  },



      components: {
        assessments_fee: function() {
          if(this.assessments_fee != null || '') this.assessments_period = "per Month";
        }
      },

Solution

  • you can create a watcher for assessments_fee so when its not null assessments_period= 'per Month'.

    Here is how you can do it:

    new Vue({
      el: '#app',
      data() {
        return {
          now: new Date().toISOString(),
          document: {
            assessments_fee: '',
            assessments_period: '',
          }
        }
      },
      computed: {
        assessmentsFee() {
          return this.document.assessments_fee
        }
      },
      watch: {
        assessmentsFee() {
          this.document.assessments_fee ?
          this.document.assessments_period = "per Month" :
          this.document.assessments_period = ""
        }
      }
    })
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
    <div id="app">
      <div class="col-lg-6">
        <div class="form-group">
          <label>Assessment Fee <span class="note">C</span></label>
          <input type="text" class="form-control" v-model="document.assessments_fee" placeholder="$">
        </div>
      </div>
      <div class="col-lg-6">
        <div class="form-group">
          <label>Assessment Period <span class="note">C</span></label>
          <input type="text" class="form-control" v-model="document.assessments_period" placeholder="(per month/quarter/etc.)">
        </div>
      </div>
    </div>