OnClick JavaScript not working with "line break"

0

I am a problem, I get an information entered by the user in the database and put it in a button with onclick for the user to click put it in a modal so that it can edit, I can not put directly in the modal because they are several information then and they have to be changing depending on where the user clicks, the problem arises when it comes information with line break, (the user used an enter during insertion of the information), onclick simply does not work and gives an alert: "Uncaught SyntaxError: Invalid or unexpected token" . works normally, only with the line break that does not. Here is the code for the button:

<button class="btnEdit btn btn-primary btn-xs" onclick="editar(\'' + value.nome + '\' , \'' + value.modulo + '\' , \'' + value.roteiro + '\' ,' + value.id + ')"  ><i class="fa fa-edit"></i></button>';
    
asked by anonymous 25.08.2017 / 21:43

2 answers

1

So you have to try to remove these line breaks so you do not generate a " constante de cadeia não finalizada " error in javascript.

To avoid this error, you can eliminate line breaks when you "pick up the information entered by the user in the database", ie in the select, as follows:

SELECT REPLACE(
REPLACE(NOME-COLUNA, '\r', '\r'),
'\n',
'\n'
) FROM NOME_TABELA;

Example: used table

Result:

InJavascriptisasinglelinewithoutbreakingandthereforewithoutgeneratingerrors.

console.log("Essa coluna tem\r\nquebra de linha\r\nduas veazes\r\nponto final");

However with line break ...

 console.log("Essa coluna tem\r\nquebra de linha\r\nduas 

        veazes\r\nponto final");
    
26.08.2017 / 00:42
0

When writing HTML, line breaks should be represented by "\ n", so you must replace the line break characters with "\ n". Example:

<html>
<body>
  <input type="button" onclick="alert('Oi, \nTudo bem com você? \n Espero que sim.');" value="Botão" />
</body>
</html>

Replace example if you generate the HTML code from Javascript:

variavel.replace("\n","\n") 
    
25.08.2017 / 22:02