Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

261
Views
Vue track the index of questions in form

I have a component 'form' which contains of 6 questions. I have a plugin for generating phone input. The phone question is 3rd and I need to generate it only when the number of question is 3rd. I have an index of questions in data() hook, so I need to call function generateTel() when the index is 3 because this input creates in DOM only when the index is 3.

The html looks like:

<div v-if="index === 1" class="question"></div>
<div v-if="index === 2" class="question"></div>
<div v-if="index === 3" class="question"></div>
<div v-if="index === 4" class="question"></div>
<div v-if="index === 5" class="question"></div>
<div v-if="index === 6" class="question"></div>

How can I track when the index equals 3? Or maybe it isn't the best practice and I shouldn't use v-if?

Note: the Bergur's solution works but I had to add some corrections:

watch: {
 index(newVal) {
  if (newVal === 3) {
     let ref = this // to avoid this.generateTel() is not a function error
     setTimeout(function () { // add setTimeout to avoid calling method on undefined 
       ref.generateTel()
     }, 100)
  }
 }
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Use watch: https://vuejs.org/v2/guide/computed.html#Watchers

In your component:

watch: {
  index(newVal) {
    if (newVal === 3) {
       this.generateTel()
    }
  }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!