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

151
Views
How can I set input value only once to dynamic value and then keep the modified value?

in a for loop I've an input element (type number) whose value I want to modify by a decrease and increase button:

<div class="s-featured-list__item s-featured-list__item--expandable" v-for="(item, itemIndex) in category.items" :key="item">
    <button class="button--decrease" @click="decreaseInput(catIndex, itemIndex)">
      <svg width="16" height="16" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg">
        <use href="#icon-minus-16" xlink:href="#icon-minus-16"></use>
      </svg>
    </button>
    <input class="stepper__input" :ref="'fooditem_' + catIndex + '_' + itemIndex" :value="item.min !== '' ? item.min : 1" :min="item.min !== '' ? item.min : 1" type="number" >
    <button class="button--increase" @click="increaseInput(catIndex, itemIndex)">
      <svg width="16" height="16" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg">
        <use href="#icon-plus-16" xlink:href="#icon-plus-16"></use>
      </svg>
    </button>
    </div>

I am setting the min attribute conditionally and also want to set the initial value of the element to the correct minimum value.

This works fine and also decreasing and increasing works fine with the following methods:

<script>
export default {
...
  methods: {
    decreaseInput(catIndex, itemIndex) {
      const item = this.$refs[`fooditem_${catIndex}_${itemIndex}`];
      if(item && item[0] && parseInt(item[0].min) < parseInt(item[0].value) ){
        item[0].value = item[0].value - 1;
      }
    },
    increaseInput(catIndex, itemIndex) {
      const item = this.$refs[`fooditem_${catIndex}_${itemIndex}`];
      if(item && item[0]){
        item[0].value = parseInt(item[0].value) + 1;
      }
    }
  }
}
</script>

My problem is, that whenever the component renders again (e.g. because I'm modifying a value or whatever), the value of the input is set back to the minimum value.

How do I set the value only once to the minimum value and then keep the modified value by the user?

Thanks in advance for your help!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can declare a variable and set it value to minimum value and then whenever component render again it will set it value to its minimum value. Hope you get what you want .

about 4 years ago · Juan Pablo Isaza Report

0

Easiest solution should be using v-once template directive.


See also VueJS render once into an element.

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!