Good day! Plz tell me in this question. The database stores text framed with <p></p> tags. This text is pulled from the database using ajax and inserted into the textarea field using

 $("textarea").html(res); 

The problem is that in the end, in textarea, the resulting text is displayed along with the <p></p> tags. How to make it so that instead of <p> were full paragraphs? <p></p> in the database specifically left to save paragraphs in the text

    2 answers 2

    The textarea element does not know how to display html markup. If you want to get a paragraph resemblance in the textarea element, then you have to replace all these tags with regular line breaks and spaces.

     var str = "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras eu odio quis mauris consequat scelerisque id tempor diam.</p><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque sed condimentum purus. Mauris blandit justo a laoreet blandit. Quisque sit amet ullamcorper justo. Donec mollis convallis odio sit amet aliquet. Maecenas vitae purus et elit dapibus efficitur. Duis venenatis neque ut tincidunt gravida. Donec feugiat mauris non tincidunt suscipit.</p><p>Nunc euismod ullamcorper risus at semper. Morbi sit amet magna posuere, malesuada ligula vel, efficitur nulla. Donec non ipsum non sem porta finibus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Donec ultrices aliquet arcu, dictum vestibulum tortor.</p>"; var edited = str.replace(/<\/p>/g,"\n").replace(/<p>/g," ") $("#textarea").html(edited); 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <textarea id="textarea" cols="80" rows="12"></textarea> 

    • tell me what does / g mean? - aleks_sk
    • @aleks_sk, a regular expression flag for global search and replace. Otherwise, it will replace only the first element found and will not go on: javascript.ru/String/replace - Alex Krass
    • Thank you for your reply, please tell me how to insert the code here, otherwise I am on this site for the first time - aleks_sk
    • @aleks_sk, where the editing tools (bold, italic, link, image, etc) when creating a message at the top have curly brackets { } to highlight a code or символ страницы с угловыми скобками to create a starting example like in my answer. If in the comments, then click "help" to the right of the comment window when directly writing it. - Alex Krass
    • ok thanks for the help! - aleks_sk

    To do this, you need to install on the website a visual editor (WYSIWYG), for example ckeditor . And in this case - when inserting html-code into the text area - it will be picked up by the visual editor and designed as you need, with the ability and edit.

    • added ckeditor. Now it’s impossible to insert an answer to this wysiwyg. It generates its markup, but I understand that it is not in the DOM and therefore jquery does not find the element and does not insert the answer. $ (". cke_editable> body> p"). html (res); - aleks_sk
    • The visual editors for this case have an API, the Ckeditor too. You can paste the code into it with the command CKEDITOR.instances['mytextarea1'].setData(html) , where mytextarea1 is the id of your text area to which Ckeditor was attached, and html is your code obtained via AYAX. - Vitaly Emelyantsev
    • Thanks, earned. Now the task is to send the Ajax edited text. Do not tell me how to implement it? Otherwise I can’t figure out the ckeditor documentation (( - aleks_sk