As here on a stack made or in many forums. I write the text, then select one word and press the B button or add a link and bold or just this word becomes the answer to the question how to catch the highlighted word: https://stackoverflow.com/questions/275761/how-to-get-selected -text-from-textbox-control-with-javascript

But how then to surround it with tags b for example. Replacing regular? And if he singled out the word "so" and in the text there are 3 words "so" in different places?

    1 answer 1

    You just need to wrap the selected line and place it between the lines that are before and after the selection.

    Example:

    function wrapFn(str) { return '<b>' + str + '</b>' } function replaceSelection(el) { if (el.selectionStart === el.selectionEnd) return; var text = el.value; var start = el.selectionStart; var end = el.selectionEnd; var wrappedValue = wrapFn( text.substring(start, end) ); el.value = text.substring(0, start) + wrappedValue + text.substring(end); } var el = document.querySelector('#myTextarea'); replaceSelection(el); 
    • Something she immediately adds before the selection of any text jsfiddle.net/cjxfoayx - marrk2
    • Well, I generally call it in an example in order to simply show what should go in there. Personally for you, added a condition at the beginning of the method - Tankerxyz