Javascript - Populate a field with paragraph

Asked

Viewed 135 times

3

I am developing a javascript to popular a text box, but I came across a situation that is causing me difficulty. Need popular with this message for example:

TEST

TEST

TEST

But I’m not getting to bring the line breaks when I populate the field, I tried with /n,<br>, </ br>, vbCrLf, but I did not succeed. Someone could help me.

Pointing out that my html text box is like this:

<td colspan="2">
            <textarea id="teste" class="CaixaTexto" runat="server" style="width: 600px;
                height: 90px;" cols="97" rows="5"></textarea>
        </td>

Att,

  • Tried to \n\r? Or even just \n?

  • I tried the \n\r and it worked. Thank you very much!

2 answers

1


Usa .replace(/\r?\n/g, '<br />'), example:

  var txtBox = $('textarea');
txtBox.keydown(function(e){
    var that = this;
    setTimeout(function(){
        var html = that.value.replace(/\n/g,"<br/>");
        $("div").html(html);
    },10);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea></textarea>
<div>

1

You said you tested /n, when it should actually be \n or \n\r.

  • Correct.. I have now tested ' n r', but my java script is not populating after the first break. Staying this way:

Browser other questions tagged

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