Sunday, June 2, 2024
 Popular · Latest · Hot · Upcoming
164
rated 0 times [  170] [ 6]  / answers: 1 / hits: 19965  / 7 Years ago, mon, january 8, 2018, 12:00:00

I have been trying to insert emoji in textarea exactly where the cursor is at. I looked around how tos in the web could not find anything specific in VUE JS. Most of them are in plain JS.



I have this Code



<div class=picker v-show=showPicker>
<click-outside :handler=handleClickOutside>
<picker
set =messenger
title=Pick your emoji…
emoji=point_up
@click=addEmoji
:emoji-size=16
>
</picker>
</click-outside>
</div>

<textarea id=greeting_text_input class=form-control
type=text
v-model=greeting_text
rows=8
required
placeholder=Hi {first-name}! Welcome to our bot. Click on the ‘Get
Started’ button to begin
>
</textarea>


My Method



addEmoji(emoji){
this.greeting_text += emoji.native;
this.showPicker = !this.showPicker;
}


Obviously, this code will add the character (emoji, in my case) to the last of the string. I need a pure vuejs solution for this.
What would be the best practise for this kind of problem in Vue? as there are few solutions in the web that based either in vanilla JS or Jquery.


More From » vue.js

 Answers
5

Two steps:


1 get textarea element using a vue-way:


1.1 Add ref attrbute to textarea tag in your template code:


<textarea ref="ta"></textarea>

1.2 get this element after mounted hook of this component:


let textarea = this.$refs.ta

2 get cursor position of textarea element.


let cursorPosition = textarea.selectionStart

Here is reference: ref


[#55518] Thursday, January 4, 2018, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
arthur

Total Points: 729
Total Questions: 107
Total Answers: 109

Location: China
Member since Mon, Aug 22, 2022
2 Years ago
;