'Passing data from database containing new line breaks jquery

In a VB.NET project I am dynamically building a jquery using data from a database. Some data are containing new line information which are breaking the script.

I tried replacing vbLf by "<br>" or encoding the "<br>" but none seems to work.

This is the code I am using to simulate my real life situation:

    Dim sString As String = "abc" & vbLf & "def" & vbLf & "ghi"
    litJS.Text = "<script>$(idHTML3).html('" & sString & "')</script>"

This code gives this result:

This code gives this result
(source: corobori.com)

What am I missing ?



Solution 1:[1]

If appears you want to user the string sString as html. In that case you need <br>\

Dim sString As String = "abc<br>def<br>ghi"

If you want to use multiple lines in javascript you need to create a line break

Dim sString As String = "abc\rdef\rghi"
litJS.Text = "<script>alert('" & sString & "')</script>"

But why not set the text to the label directly instead of using jQuery?

litJS.Text = sString

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 marc_s