Home > OS >  VUEjs, how to get value of text box based on it's id
VUEjs, how to get value of text box based on it's id

Time:11-27

i have two text boxes, both text boxes will have value.

but i want to get value of one text box based on id number which i will enter.

enter image description here

if i enter 1 it should show value of text box 1.

Here is what i tried, right now it's showing data from both text boxes.

template:

<input  v-model="textdata1" id="1" type="text" placeholder="i am id1, show my value">

<input v-model="textdata2" id="2" type="text" placeholder="i am id2, show my value">

<input v-model="searchid"  type="text" placeholder="which ids data you want, 1 or 2 ">

<button  @click="getvalue">RECEIVE</button>

<div>show value of id 1or2 here: {{id1data}}</div>

VUEJS:

<script>
import { defineComponent,ref } from 'vue'

export default defineComponent({
  setup() {
      const id1data =ref("");
      const id2data =ref("");

function getvalue(){
    
    this.id1data=this.textdata1;
    this.id2data=this.textdata2;
    
    }

return{
   id1data,
    id2data,
    getvalue,
}
}
})
</script>

CodePudding user response:

If you insist on using IDs instead, here is a way to do just that:

<script>
import { defineComponent, ref } from "vue";

export default defineComponent({
  setup() {
    const value = ref("");

    const searchid = ref("");
    const searchidinput = ref("");

    function getvalue() {
      this.searchid = this.searchidinput
      const el = document.getElementById(this.searchid);
      if (el) {
        this.value = el.value
      }
    }

    return {
      value,
      searchid,
      searchidinput,
      getvalue,
    };
  },
});
</script>

<template>
  <input id="1" type="text" placeholder="i am id1, show my value" />

  <input id="2" type="text" placeholder="i am id2, show my value" />

  <input v-model="searchidinput" type="text" placeholder="which ids data you want, 1 or 2 " />

  <button @click="getvalue">RECEIVE</button>

  <div>show value of id {{ searchid ? searchid : "<none>" }} here: 
{{ value ? value : "none selected"}}
    
  </div>
</template>

<style scoped>
a {
  color: #42b983;
}
</style>

CodePudding user response:

Here is one way to do it. You can create a pair of data values for each input.

<script>
import { defineComponent, ref } from "vue";

export default defineComponent({
  setup() {
    const textdata1 = ref("");
    const id1data = ref("");

    const textdata2 = ref("");
    const id2data = ref("");

    const searchid = ref("");
    const searchidinput = ref("");

    function getvalue() {
      this.searchid = this.searchidinput;
      switch (this.searchid) {
        case "1":
          this.id1data = this.textdata1;
          break;
        case "2":
          this.id2data = this.textdata2;
          break;
      }
    }

    return {
      textdata1,
      id1data,
      textdata2,
      id2data,
      searchid,
      searchidinput,
      getvalue,
    };
  },
});
</script>

<template>
  <input v-model="textdata1" id="1" type="text" placeholder="i am id1, show my value" />

  <input v-model="textdata2" id="2" type="text" placeholder="i am id2, show my value" />

  <input v-model="searchidinput" type="text" placeholder="which ids data you want, 1 or 2 " />

  <button @click="getvalue">RECEIVE</button>

  <div>show value of id {{ searchid ? searchid : "<none>" }} here: 
{{ searchid === "1" ? id1data : searchid === "2" ? id2data : "none selected"}}
    
  </div>
</template>

<style scoped>
a {
  color: #42b983;
}
</style>

You may also resort to using reactive() instead for brevity and organization purpose.

  • Related