I'm trying to create simple app with Vuejs that will check length of title tag (px).
Inside mounted I set the default value for title and run check length of default title tag but it return 0. When I change value of input, it work fine.
Here is my code. Link: https://codepen.io/mrtienhp97/pen/LeOzGa
HTML:
<div id="app">
<input v-model="title" @input="titleCheck()">
<div id="title-preview">{{title}}</div>
<div class="message">{{message}}</div>
</div>
CSS:
#title-preview {
display: inline-block;
}
JS:
new Vue({
el: '#app',
data: {
title: '',
message: ''
},
mounted: function() {
this.$nextTick(function () {
this.title = "Default Title";
var title_width = document.getElementById("title-preview").offsetWidth;
this.message = title_width + 'px';
});
},
methods: {
titleCheck: function() {
var title_width = document.getElementById("title-preview").offsetWidth;
this.message = title_width + 'px';
}
}
})
Can anyone help me correct it?