I'm working with a portfolio project, where users can upload a picture, provide a description below it, and then click the "add" button to add another image and description.
I'm trying to add a character counter to the description input, which is a textarea
field. Usually I can add the name of the v-model
into the function, and it works fine, but this textarea
is in a for
-loop, so I'm not sure how to get this function to work.
Template:
<div class="newPortfolioList">
<div class="newPortfolioItem" v-for="(item, index) in this.portfolioItems" v-bind:key="index">
....
<div class="newPortfolioDescription">
<textarea v-model="item.portfolioDescription" @keyup='remaincharCount()' maxlength="1000" placeholder="Item Description..."></textarea>
</div>
<!-- Displaying the remaining characters -->
<span style="text-align:left; padding: 10px;">{{ remaincharactersText }}</span>
</div>
...
Script:
export default {
data () {
return {
portfolioItems:[],
maxcharacter: 1000,
remaincharactersText: "1000 characters remaining"
}
},
methods: {
createPortfolioItem () {
this.portfolioItems.push({
portfolioDescription: ''
})
},
remaincharCount () {
if (this.foo.length > this.maxcharacter) {
this.remaincharactersText = "Exceeded "+this.maxcharacter+" characters limit.";
} else {
var remainCharacters = this.maxcharacter - this.foo.length;
this.remaincharactersText = remainCharacters + " characters remaining";
}
}
}
}