I have a textbox
<div>
<b-form-textarea
id="textarea"
v-model="text"
placeholder="Certification text "
rows="5"
max-rows="6"
></b-form-textarea>
</div>
And two buttons
<b-button variant="primary" class="btn btn-primary btn-lg top-right-button mr-1">Save</b-button>
<b-button variant="info" class="btn btn-info btn-lg top-right-button mr-1">Add Name</b-button>
When the user is typing and in between if he clicks Add Name button {name} should be placed in the textbox (Where ever the cursor is). How can i implement it?
You can add a
ref
to your textarea, and access theselectionStart
property, which contains the placement of the cursor. You can then use this index to splice the given text into the textarea's text at the given position.And since clicking the button will lose the focus of the input, you can add it back by calling the
focus
method on the ref, along with setting theselectionStart
andselectionEnd
so the cursor is where it left off.