I’m trying to implement an ‘edit’ button on a django website using javascript. I’m very new to javascript though.
In django it’s pretty easy to split the text into paragraphs, you just code it like this {{ text|linebreaks }} and django adds <p>
tags dynamically. To make the transition smooth (after sending fetch request and receiving response) with javascript I need to create a function that loops through the edited text and generates <p>
tags as well.
However I dont’ even know where to start. How exactly do you find where each paragraph ends using javascript?
2
Answers
From the docs, the definition is (although not clear at first as in this definition) that new line gets a
<br>
whereas double gets a<p>
you can split the text into paragraphs based on line breaks by using
<br>
. Let’s assume that after fetching the edited text from Django, you have it stored in a variable callededitedText
. To generate<br>
tags for each paragraph, you can follow these steps:Split the text into an array of paragraphs using the
split()
function.Loop through the array of paragraphs and join them using
<br>
tags to generate the new formatted text.Hope this helps