2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Vue.jsで管理するtextareaをごにょごにょしたかった

Last updated at Posted at 2021-10-08

概要

textareaの中身を外からJSでいろいろする画面をVuetifyで作ろうとするとハマる。
実はVuetifyは関係なくてVue.jsなら(たぶん)ハマる。

やろうとしたこと

あるボタンを押すとカーソル位置にテキストが差し込まれるよくあるやつ。

わりとすぐわかったこと

textarea.setTextRangeはVue.jsで管理しているtextareaでは当然うまく動かない。
textarea.selectionStarttextarea.selectionEndでカーソル位置をとってきて、v-modelを直接編集する必要がある。

ハマったこと

textareaのテキスト編集後のカーソル位置がいい感じにならない。
textarea.setSelectionRangeがまともに動いていないように見える。

setTimeout(() => bodyTextArea.setSelectionRange(2, 2))
this.$nextTick(() => {
   textarea.setSelectionRange(2, 2))
})

ふーんなるほどね

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?