search

Home  >  Q&A  >  body text

Vue js: Get the input string and display it, and also get the length of the string

I want to get a string input and display it, along with the number of characters of the string.

Need help to implement.

<section id="app">
      <h2>Learn  Vue Works</h2>
      <input type="text" @input="saveInput">
      <button @click="setText">Set Text</button>
    <br>

   <p>{{ qry }} {{ message }}</p>

====app.js======================================

const app = Vue.createApp({
  data() {
    return {
      message: 'Vue is great!',
      qry: 'Query String : ',
      currentSearchInput: '',
    };
  },
  methods: {
    setText() {
      this.message = this.currentUserInput;
    },
   saveInput(event) {
      this.currentUserInput = event.target.value;
   },
  },
});

app.mount('#app');

Thanks in advance.

P粉785522400P粉785522400302 days ago533

reply all(1)I'll reply

  • P粉788765679

    P粉7887656792024-02-22 14:09:16

    Use v-model to bind currentSearchInput to the input. You don't need to saveInput at all.

    
    {{currentSearchInput}} {{currentSearchInput.length}}
    
    setText() {
          this.message = this.currentSearchInput;
        },
    

    Your code has currentSearchInput and currentUserInput. typo?

    reply
    0
  • Cancelreply