Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

324
Visualizações
Textarea value not retaining line breaks from variable

I have a textarea that needs to retain the submitted value and format after submission, but for some reason \n does not seem to get passed from the variable php variable.

$text = '1\n2\n3';

echo '<script>
var text = "'.$text.'";
$( document ).ready(function() {
    $("textarea").attr("placeholder","1) Start Each Comment On A New Line...");
    $("textarea").val(text);
});

</script>'

The variable is passed from php to JS. When I echo the variable I get the expected result:

1\n2\n3

When the variable is assigned to the textarea value the result is:

123

Instead of:

1
2
3

Now if I change the js to the following I get the expected result with each number on a new line.

$("textarea").val("1\n2\n3");

Does anyone know why the php variable is not passing the \n returns to JS?

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Using json encode should escape your line breaks in a way JS will understand.

$text = json_encode('1\n2\n3');

echo '<script>
var text = JSON.parse("'.$text.'");
$( document ).ready(function() {
    $("textarea").attr("placeholder","1) Start Each Comment On A New Line...");
    $("textarea").val(text);
});

</script>'
about 4 years ago · Santiago Trujillo Relatório

0

You should use this.

    <script   src="https://code.jquery.com/jquery-3.1.1.min.js"   integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="   crossorigin="anonymous"></script>

<textarea rows="4" cols="50">
</textarea>


<?php

$text = '1\n2\n3';

echo '<script>
var text = "'.$text.'";
$( document ).ready(function() {
    $("textarea").attr("placeholder","1) Start Each Comment On A New Line...");
    $("textarea").val(text);
});

</script>'
?>

enter image description here

about 4 years ago · Santiago Trujillo Relatório

0

The problem is you are expecting Browser Javascript to understand PHP's "\n" character. That will not work. Also, your code uses the single quote to denote the newline character. PHP requires that you use double quotes to denote the newline special character.

There are at least two ways you can fix this.

The first is to use PHP's nl2br() function. In that case, your code could be rewritten as:

$text = "1\n2\n3"; //notice the use of double quotes.

echo '<script>
var text = "'. nl2br($text).'";
$( document ).ready(function() {
    $("textarea").attr("placeholder","1) Start Each Comment On A New Line...");
    $("textarea").val(text);
});

</script>'

A second way is to use the HTML
tag directly in your variable declaration. This would obviate the need to use PHP's nl2br() function.

$text = '1<br>n2<br>n3';

echo '<script>
var text = "'.$text.'";
$( document ).ready(function() {
    $("textarea").attr("placeholder","1) Start Each Comment On A New Line...");
    $("textarea").val(text);
});

</script>'

Either of both approaches should work.

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda