I am trying to create a "twitter-like" character counter using Vuejs; however, I am encountering two separate issues.
HTML:
<div class="form-group" id="vue">
<label for="eligibility-address-detail">Address Notes:</label>
<textarea class="form-control" id="eligibility-address-detail" rows="3"
name="eligibility[address][detail]" v-model="eligibility.address.details"
v-on:keyup="limiter(this, 140)" required></textarea>
<span class="limiter"></span>
</div>
JavaScript:
var main = new Vue({
el: "#vue",
data: {
eligibility: {
address: {
details: ""
}
}
},
methods: {
limit: function(elem, limit){
var chars = elem.value.length;
if (chars > limit) {
elem.value = elem.value.substr(0, limit);
chars = limit;
}
$(elem).siblings('.limiter').html((limit - chars) + " / " + limit + "characters remaining");
}
}
});
In general, with most modern front-end frameworks (Angular, React, Vue, etc) you want to avoid manipulating and inspecting the DOM manually. The recommended approach is to make everything data-driven (aka: use the model) and let the framework update the DOM when needed - this is a key concept behind the whole "reactivity system"
But here are a few recommendations to fix your issue:
Do not call your limit()
method on DOM events. Instead... look at the eligibility.address.details
attribute which is are binding to the inputs v-model
.
You can create a computed property that calculates the remaining characters based on that attribute.
computed: {
charactersLeft() {
var char = this.eligibility.address.details.length,
limit = 140;
return (limit - char) + " / " + limit + "characters remaining";
}
}
Then in your markup, you would use the computed property like a regular data property:
<span class="limiter">{{charactersLeft}}</span>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With