I
I
ilovemaryjane2018-05-10 20:39:29
Vue.js
ilovemaryjane, 2018-05-10 20:39:29

How to access DOM element with Vue?

For example, there is a component and this is what is in ittemplate

<div>
   <textarea></textarea> 
   <span></span>
</div>

Through this.$elI can access the whole root component, but how can I access only one textarea
How to change it in the most appropriate way for Vue, what are the best practices?
autosize(document.querySelectorAll('textarea'))

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vadim Belkin, 2018-05-10
@ilovemaryjane

<div>
   <textarea ref="textarea"></textarea> 
   <span></span>
</div>

Set the ref attribute for the desired element and refer to this.$refs using the set value
About refs in the documentation

A
Anton Anton, 2018-05-10
@Fragster

what are the best practices?

Change elements only through related data, avoid direct DOM manipulation

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question