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

3

I have a comment plugin for WordPress which has a small problem.

It works like this:

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

How the HTML code is:

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

Javascript on Pastebin

Images:

Comment example: Howitgetsaftercommented:

Is there any solution for this?

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

Once commented, it is:

I will break the

line

    
asked by anonymous 07.07.2014 / 22:42

2 answers

8

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

Switching:

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

by:

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

You should resolve the issue.

    
07.07.2014 / 23:37
1

I do not work with PHP and much less with wordpress, my area is ASP.NET and C #, but in your case I think I'll know how to give you the solution.

In any text field in html (imput, textarea) when receive is sent the text to the server the breaks of lines are brought with special characters in the case of the line break is a \n ( link ). The database should be storing like this.

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

Function in PHP to do the replace:

$resultado = str_replace("\n", "<br/>", $variavel_com_o_texto);
    
07.07.2014 / 23:21