<div> that works like a <textarea> does not break line when it is sent

Asked

Viewed 1,227 times

3

I’m with a comments plugin for WordPress who’s in a little trouble.

It works that way:

Is a <div> that works as if it were a <textarea>, only because it works like this, when the person wants to type a comment and she gives a "enter" to break the line and then send the comment, the comment arrives without breaking line, it arrives as if the person has not given a break.

What the HTML code looks like:

<div class="commentator-textarea" placeholder="Junte-se" contenteditable=""></div>

Javascript no Pastebin

Imagery:

Comment example: As it is after commented: inserir a descrição da imagem aqui

Is there any solution to this?

If I comment and break the line using Vou quebrar a<br>linha it works.

After commented is:

I’m gonna break the

line

  • 2

    From what I’ve seen, the attribute contenteditable uses tags <br> for line breaking, so theoretically it should work. You have to see how the comment code is coming in PHP. Sometimes Javascript may be taking the automatically generated tags and this is why it is going wrong.

  • @Kazzkiq <br> in the comment. .

  • Whenever Voce submits the text to the server replace the '<br/>' with ' n'

2 answers

8


The problem is that you are using the function .text() jQuery to send the comment. This function, unlike the .html(), takes only the text of the element you have defined, and delete any HTML tags that are together.

Exchanging:

$form.find('.commentator-textarea').text()

for:

$form.find('.commentator-textarea').html()

Should solve the problem.

  • What line is that on? Where did you find ?

  • 2

    On the line 11 of your Pastebin.

  • Yes! I will now test :D

  • Êbaaaaaaaaaaaaaaaaaaaaaaaaaaaaa!!! Funciounou, you’re the man! Vlw thank you so much! loooooooooool Vlw man! rsrs

  • 1

    I’m glad I could help. The good thing about breaking your head with a problem for a long time is that you can identify it right away on other occasions (believe me, I’ve suffered a lot with this .text()). A good tip is to always find out if the function used has any however or some limitation to avoid this kind of annoying little mistake. :)

  • Very cool! Thank you very much! Great :P

Show 1 more comment

1

I don’t work with PHP and much less with wordpress, my area is ASP.NET and C#, but in your case I think I will know how to give you the solution.

In any html text field (imput, textarea) when receiving text is sent to the server the line breaks are marked with special characters in the case of line break is a \n (http://en.wikipedia.org/wiki/Newline). In the database should be storing so.

Solution, you should treat the delivery (text presentation) simply by giving a replace of \n for <br/>.

PHP function to replace:

$resultado = str_replace("\n", "<br/>", $variavel_com_o_texto);
  • 2

    This would work if the field was a common textarea, but it is a div with contenteditable.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.