alex
alex

Reputation: 7601

How to select the first character of the next word in a textarea?

Right now, I'm selecteing the first character (a) of the first word inside the text area like this:

  <textarea ref="inputEl">abc def ghi</textarea>


  methods: {
    setInitialCursorPosition () {
      const inputEl = this.$refs.inputEl
      inputEl.focus()
      inputEl.setSelectionRange(0, 1)
    }
  },

  mounted () {
    this.$nextTick(() => {
      this.setInitialCursorPosition()
    })
  }
}

How to do it so I select the first character of the next word inside the same text area (d and so on)? Pretty much like the w key in the Vim editor.

Upvotes: 0

Views: 90

Answers (1)

Mohammed Elhag
Mohammed Elhag

Reputation: 4302

var str     = document.getElementById("textarea").value;
var match = str.match(/\b(\w)/g);              
var first = match.join('');  

console.log(first[1]);
<textarea id="textarea" ref="inputEl">abc def ghi</textarea>

Upvotes: 1

Related Questions