Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Pesky Line breaks…

  • This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #196751
    kamaboko
    Participant

    Hello,

    I’m having a problem with PHP and JS(not Jquery, so I ask that you please stay with vanilla JS if this is part of the solution).

    The setup:
    I have a form that is taking user input and putting it into a MySQL database. Next, I’m taking that input and populating another form using a variety of DOM elements I’m creating on the fly. In this case, a table of information. Then, I’m populating the <td> tags with data from MySQL. For instance, <td><?php echo $something ?></td>

    The problem is that much of the user input contains line breaks, and JS is not playing ball as it doesn’t recognize them. Essentially, instead of getting line breaks, I’m getting <br /> tags in their place on the form.

    For example, the original input may be…

    The fox ran up the street.
    The chicken ran down the street.

    JS is outputting…
    The fox ran up the street.<br />The chicken ran down the street.

    Question: How do I get rid of the <br /> tags and retain the line breaks the user made during the input?

    Thanks in advance,
    K

    #196769
    kamaboko
    Participant

    I wanted to reply with a clearer picture as to what I’m doing, but how does one insert snippets of code?

    #196771
    kamaboko
    Participant

    Hi,

    Thanks for your reply and the code snippet bit. I should have seen that.

    If I create a static table in HTML such as the one below, everything is fine.

    &lt;table&gt;
    &lt;tr&gt;
    &lt;td&gt;&lt;span class="job_post"&gt;Company Overview:&lt;/span&gt;&lt;br /&gt;&lt;?php echo $company_overview; ?&gt;&lt;br /&gt;&lt;br /&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;/table&gt;

    However, if I create a dynamic table via the DOM and import the text, then all goes whacko. I get the line breaks.

    var text3 = document.createTextNode('&lt;?php echo $job_description; ?&gt;');

    I have tried the nl2br which works fine for the first example, but not for the second. I’ve also tried the following to second example, but with no luck.

    $company_overview = str_replace(array("\n","\r"), "", $company_overview);

    I hope this helps.

    #196780
    kamaboko
    Participant

    Hi nkrisc,

    I typed up a huge overview of what I’m trying to do, but after reading it, I thought it may be even more confusing. At the end of the day, what I’m trying to find out is, is it possible to populate a dynamically created DOM element using php and keep the line break tags out of the displayed content.

    var txt = documentCreateTextNode('&lt;?php echo $something; ?&gt;;');

Viewing 4 posts - 1 through 4 (of 4 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.