I can’t break line using Javascript

Asked

Viewed 100 times

0

Good morning, I’m having some difficulty trying to break a line using [b] n[/b] javascript. The code is working perfectly, what happens is that when it displays, it looks like this:

My nick: ViniciusnHow I found the server: YoutubenI want to play because: I like the server

Like n is not breaking the line, but it appears there at the end of every string I put, I wonder what is happening?

<script>
    $(function () {
        $('[name="post"]').click(function () {
            var nick = $('#field0').val();
            var descobriu = $('#field1').val();
            var motivo = $('#field2').val();
            $('input[name="subject"]').val('[ Whitelist] ' + nick + ' ');
            $('input#message').val('[b]Meu nick:[/b] ' + nick + ' <br>[b]Como descobri o servidor:[/b] ' + descobriu + ' \n[b]Eu quero jogar porque:[/b] ' + motivo);
        });
    });
</script>

  • use <br> instead of n

  • 1

    I tried to use <br> but in my forum it appears the <br> tag instead of breaking line

  • Have you tried '\\n'?

  • 1

    It worked, thank you very nicematt!

  • Hey now it’s not working anymore, ta appearing like this http://i.imgur.com/Rwwmnrj.jpg

  • I put the two bars \

  • <script>&#xA; $(function () {&#xA; $('[name="post"]').click(function () {&#xA; var nick = $('#field0').val();&#xA; var descobriu = $('#field1').val();&#xA; var motivo = $('#field2').val();&#xA; $('input[name="subject"]').val('[ Whitelist] ' + nick + ' ');&#xA; $('input#message').val('[b]Meu nick:[/b] ' + nick + '\\n[b]Como descobri o servidor:[/b] ' + descobriu + '\\n[b]Eu quero jogar porque:[/b] ' + motivo);&#xA; });&#xA; });&#xA;</script>

  • I don’t know either, it seems he’s not reading the first bar

  • http://stackoverflow.com/questions/8627902/new-line-in-text-area?answertab=active#tab-top

Show 4 more comments

2 answers

3

I don’t see much point in wanting to use line break in input. In this case, the appropriate would be a textarea.

Just use \n to break the lines, see working:

var nick = 'XYZ';
var descobriu = 'por acaso';
var motivo = 'achei legal';

$('#message').val('[b]Meu nick:[/b] ' + nick + '\n[b]Como descobri o servidor:[/b] ' + descobriu + ' \n[b]Eu quero jogar porque:[/b] ' + motivo);
#message {width:80%}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<textarea id="message"></textarea>

  • So, replace for textarea only that in my forum does not work at all \n it appears as a string

  • It may be that the problem is in the display part, I answered on top of the code that is in the question, which only shows the input of the data. It may be that your forum has some conversion when recording the data, but without seeing the code, it is complicated. If you can’t find a solution to correctly view or save the data, open a question about the view, but put the part of the code that shows the data problem on the screen.

1


Instead of putting in an input, define a <p id="paragrafo"> <p> then you use your call by changing the element id:

 $('paragrafo').val('[b]Meu nick:[/b] ' + nick + ' <br>[b]Como descobri o servidor:[/b] ' + descobriu + ' \n[b]Eu quero jogar porque:[/b] ' + motivo);
  • I had already resolved that doubt, yet I will give a pleasure in your answer if someone has the same question as me.

  • beauty! Hugs.

Browser other questions tagged

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