1

I have 2 input tags

Title:
    <b-input
      style="width: 50%"
      v-model="value.title"
      class="input-element"
      placeholder="Title"
      v-on:click="greet"
    />
Id:
    <b-input
      id="id"
      style="width: 50%"
      class="input-element"
      placeholder="Id"
      v-model="value.id"
    />

Whenever I write something in ,I want the value of input to be updated the same as value of title in lowercase.

8
  • 1
    Can you please elaborate on your question. What is it you want to do? And what have you already tried so far? Commented Dec 18, 2019 at 12:33
  • I want to change the value of one input box based on other input box's value. I want to use something like document.querySelector("id").innerHTML , but innerHTML isn't working on input. Commented Dec 18, 2019 at 12:35
  • So if you put TEST into the title input, it should be reflected in the id input, but shown as test? Commented Dec 18, 2019 at 12:39
  • @Djip yes. Exactly that's what I want Commented Dec 18, 2019 at 12:40
  • 1
    What should happen then if the user writes anything into the ID input? Commented Dec 18, 2019 at 12:41

2 Answers 2

1

You can use a watcher to watch for any changes made to title and set the id to the lowercase value.

This allows the user to still manually update the ID input. While having it overridden whenever the title changes.

new Vue({
  el: '#app',
  data() {
    return {
      value: {
        id: "",
        title: ""
      }
    }
  },
  watch: {
    'value.title'(newVal) {
      this.value.id = newVal.toLowerCase();
    }
  }
})
<link href="https://unpkg.com/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://unpkg.com/[email protected]/dist/bootstrap-vue.css" rel="stylesheet"/>

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.10/vue.js"></script>
<script src="https://unpkg.com/[email protected]/dist/bootstrap-vue.js"></script>

<div id="app" class="p-3">
  <b-btn @click="value.title = 'TEST'">Set title to TEST</b-btn><br />
  <label>Title: </label>
  <b-input v-model="value.title"></b-input>
  <label>ID: </label>
  <b-input v-model="value.id"></b-input>

  <div class="mt-2">
  Title: {{ value.title }}<br />
  ID: {{ value.id }}
  </div>
</div>

Sign up to request clarification or add additional context in comments.

Comments

1

You can use a computed field that changes the title to lowercase and then make the v-model of the id input to use that computed field

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.