Benjamin
Benjamin

Reputation: 2123

jQuery appending & prepending some texts to selected words on textarea

I have a textarea and one button above this textarea. I want to select a word(s) in this textarea and prepend + append some signs to selected word(s).

For example:

hello -> (hello) // i want to prepend "(" and append ")" when click one button above or below textarea. But i don't know how to do it with selected words?

Upvotes: 0

Views: 291

Answers (1)

Caleb Gray
Caleb Gray

Reputation: 3097

I highly recommend that you use: jQuery - fieldSelection

It has the functions: getSelection() and replaceSelection(text)

So you could do this:

var before = $(this).getSelection();
$(this).replaceSelection(prepend + before + append);

Upvotes: 1

Related Questions