Ave! How can I replace selected text with jquery? Let me have a text - "Eat my soft buns and snack with a dumpling." I highlight the word "soft" and click on the [ <b> ] button. The highlighted word is cut out and entered into the f-tion where tags are added to it and put in place of the flashing cursor. As a result, we have the html line: - " Съеш моих <b>мягких</b> булочек да закуси вареничком ".

I do not ask you to do everything for me as in TZ. I just need to specify the path by which to go in order to gain this knowledge.

Ps all buns ! [dango

  • I do the editor in the browser - Ezdrael
  • Why do you need it through jQuery? Wrapping a tag is a fairly simple operation. In Sublime Text select the desired text → Alt + Shift + W → where the cursor blinked, enter the name of the tag. The tag itself will close for you. An example . Thank. - Sasha Chernykh
  • one
    I make a visual text editor. alltrades.ru/img/news/243.png - Ezdrael
  • So em no buns I removed some female visitor UTB. I will return - Ezdrael
  • 3
    Did not even try to look? Here, the first reference and even with an example - Deonis

1 answer 1

 foo=function() { var selection= window.getSelection().getRangeAt(0); var selectedText = selection.extractContents(); var span= document.createElement("span"); span.style.backgroundColor = "yellow"; span.appendChild(selectedText); selection.insertNode(span); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam rhoncus gravida magna, quis interdum magna mattis quis. Fusce tempor sagittis varius. Nunc at augue at erat suscipit bibendum id nec enim. Sed eu odio quis turpis hendrerit sagittis id sit amet justo. Cras ac urna purus, non rutrum nunc. Aenean nec vulputate ante. Morbi scelerisque sagittis hendrerit. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.</p> <input type="button" onclick="foo();" value="Выделить"/> 

  • If helped you, mark as the answer - Abmin