Insert new line into javascript line

I read this question / answer and this one , but I can’t get it. Work in my situation.

I am creating a list of names from the json array returned from php script. After the code below, I put the goTosList string in the label through jquery. I need each name to be on a new line.

The following code simply prints

var goTosList = ''; if (data !== 'empty') { // Build the go tos list as a comma-separated string $.each(data, function(index,element) { goTosList += (element['name'] === undefined ? '' : element['name']) + '\n\r'; }); } 

I tried only \r and just \n and without single quotes. I can not make it work.

+6
source share
1 answer

If you output HTML and you want newlines to have two options:

Use the <pre> as a wrapper for your text (not suitable here, I think)

 <pre>some Text with newlines</pre> 

or add <br> instead of \n\r :

 some Text<br>with newlines 

So in your code this will translate to

 goTosList += (element['name'] === undefined ? '' : element['name']) + '<br>'; 

and then paste this into the DOM on

 $('#yourLabel').html( goTosList ); 
+11
source

Source: https://habr.com/ru/post/946013/


All Articles