Skip navigation
Currently Being Moderated

Break Line with JS

Jun 3, 2012 7:40 PM



I use cfset to set a message with several lines using "\n" in CFC. For example:

<cfset message = message & #JSStringFormat( & '\n'>


Then, when I use alert('#message#') in CFC, it shows the message with several lines correctly.


Then, I use:

document.getElementById('nameList').innerHTML = '#message#';

to return the name list to CFM page.


However, when I use alert(document.getElementById('nameList').innerHTML) in CFM, it shows message incorrectly. it only shows me all name with a delimiter " ".


It seems the line break character "\n" cannot work correctly. Can anyone help me to solve this problem?

  • Currently Being Moderated
    Jun 4, 2012 5:15 AM   in reply to Phinehas1234

    Once you set the innerHTML property then read it, you get back HTML not a Javascript string.  If you want line breaks in HTML then you use the <br> tag.

    Mark as:
  • Currently Being Moderated
    Jun 5, 2012 2:23 PM   in reply to Phinehas1234

    Ok, so innerHTML returns HTML, but alert expects a javascript string.  In order for a Javascript string to show new lines you need the \n char.  But once you set the innerHTML property those are replaced with actual new line characters.  So when you read the innerHTML property to get the actual chars back.  So, you need to replace those chars with \n on the javascript side. (This is what jsStringFormat is doing for you on the server side)


    Do you follow?  You are getting the actual newline chars back but the Javascript alert needs the \n special character to display them.


    You might have better luck using an HTML modal window and just displaying the HTML as is rather than use an alert();  Most people shy away from using alert as its not very user friendly.  Something like the jQuery UI Dialog widget would work but I'm not sure if you are comfortable enough with Javascript.

    Mark as:
  • Currently Being Moderated
    Jun 6, 2012 12:49 AM   in reply to Phinehas1234



    InnerHTML is not an issue always. Please have a look into the following code. I could show the display with line break



    <cfset message = "Welcome">

    <cfset names = arraynew(1)>

    <cfset count = 1>

    <cfloop index="loopcount" from=1 to=12>

        <cfset names[loopcount]=count>

        <cfset count = count + 1>


    <cfloop array="#names#" index="name">

        <cfset message = message & ' ' & #JSStringFormat(name)# & '<br>'>


    <p id="test"></p>

    <script language="javascript" type="text/javascript">


        document.getElementById("test").innerHTML = '<cfoutput>#MESSAGE#</cfoutput>';




    Please let me know which html ctrl you are using the message to be displayed



    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points