'Make content editable div get focus on button click
I have a content editable div that I'm using to retrieve some data.
<div contenteditable="true" ref="test">/*SomeText*/</div>
After running the focus through the ref with this.$refs.test.focus(). It didn't work and I get an error on the console (Cannot read properties of undefined reading focus)
How can I trigger a focus effect on content editable div?
Solution 1:[1]
The attribute for content editable should not be content-editable="true", but instead contenteditable="true".
const App = {
el: '#app',
methods: {
focusDiv() {
this.$refs.test.focus()
}
}
}
const app = new Vue(App)
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<div contenteditable="true" ref="test">My Div</div>
<button @click="focusDiv">Focus on div</button>
</div>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
