How to add new line in string?

How to add new line in string?
0

#1

Here is code:
https://jsfiddle.net/x012yjk6/

I’m trying to achieve this:
-1
-2
-3


#2

You can add a new line in a string with "\n"


#3

take a look at jsfiddle


#4

I stand corrected, go to the demo you have and inspect the elements. The new lines are there, the problem is how you put them in the DOM.


#5

Since you use innerHTML you can use HTML < br /> for line breaks. If you want a line break in JS (for example if you use window.alert()) you use “\n”.

EDIT: Have a look at this stackoverflow question.


#6

I want to use \n for new line like i did on line 5.
In another case if i print “test\n\test” it goes to “test test”, i open dev tools and saw there is new line but how can i achieve this also in to be printed in “preview” of html, with new lines? i want to make list by adding "- " like prefix and “\n” like suffix so it goes to new line


#7

By using < br /> it will continue on a new line. Or you can wrap your final customItems in between < pre > tags: document.getElementById("ovde").innerHTML = "<pre>" + customItems + "</pre>";


#8

here: [ https://codepen.io/ustvarno/pen/VjPQdY ]
it doesn’t work with "<pre>"
line 343, how can i make it so down below when i set items to div to get something like:
test1, test2, test3

->

-test1
-test2
-test3

EDIT: if u use dev tools, just click on blue button play if debbuger shows


#9

That is strange, the code seems ok to me :confused:


#10

The code does what it is supposed to do, the problem is that he tried to manipulate a string but did not return a string, instead made it an html element. The problem is with the html element he needs to find the right way, I’m not sure if he has tried the <br> or not but it was mentioned already.


#11

This is what me and @BenGitter tried to get you to come up with: https://jsfiddle.net/x012yjk6/2/


#12

That should normally work, but @nameToReachPeople is using React. I have no experience with React, so I won’t be able to help.


#13

Well don’t feel bad, we answered the original question at least.

For the react part he should add it dynamically as in append it from the loop, but that is out of the scope of this question and I think should go into the react help category.

Take a look at https://github.com/markerikson/react-redux-links for learning react. I’m about to go dig in myself.

The link was from http://forum.freecodecamp.com/t/resources-for-learning-react-redux/672


#14

Thanks all for help, sorry i didn’t mention it is react js, i was thinking it doesn’t matter


#15

I achieve this with https://facebook.github.io/react/tips/dangerously-set-inner-html.html