0

I have a SQlite database which receives data from a HTML form. If the enter key is pressed then it is of course inserted into the database.

On another page, the information from the database is placed inside a textarea. It all works fine, however when there is a line break, this does not work and the text is not inserted into the textarea.

I'm using the Python framework Flask to input the SQlite data into the javascript and then into the HTML.

What I have tried:

I've tried replacing the the line break with the text '\n' so that the line-break is then parsed by javascript. I tried this in python using regex like so:

Python:

def removeLineBreaks(text):
    return re.sub(r'(\r\n|\n|\r)', "\\n", text)'

So the question is, how can a linebreak be placed into a HTML form (like any other character) by javascript from a SQLite database?

Update:

I made a function which converts the linebreaks from the SQLite database:

def removeLineBreaks(text) :
    newText = ''
    for char in text:
        if char == '\r\n' or char == '\r' or char == '\n':
            newText += '\\n'
        else:
            newText += char
    return newText

However, instead of one line break, there is two so that there is an empty line. If I replace newText += '\\n' with newText += '' then there is no line break at all and all the text appears on one line. So I've worked out how to make a line break but now how do you only display one like break like:

Line 1
Line 2
Line 3

Instead of displaying two like:

Line 1

Line 2

Line 3
Pav Sidhu
  • 6,724
  • 18
  • 55
  • 110

1 Answers1

0

So the question is, how can a linebreak be placed into a HTML form (like any other character) by javascript from a SQLite database?

This actually doesn't need JS, and doesn't depend on the blacking data store. Since HTML treats all whitespace basically the same, try using the entity for a newline, i.e.

<textarea>Line one&#10;Line 2</textarea>

Here's a little more info

Now, how to get Jinja to emit newlines as HTML entities is a different question.

Community
  • 1
  • 1
David Ehrmann
  • 7,366
  • 2
  • 31
  • 40