Solution 1 :

Use

n

or

br tag

for new line.

dictionary.title += word.value + "n";

Solution 2 :

Like in the above answers as said you can use <br> and n for the new line but there are many alternative ways you can achieve this for your understand I have written a code where I used a

tag which is also called a block element where it takes given width and height and add a new line at the end automatically. There are many block-level elements you can also use for new lines.

let value = document.querySelector(".newInput");
let btn = document.querySelector("button");
let div = document.querySelector(".dictionary");


function storeValues(){
let dictionary = "";
if(value.value){
  dictionary += `<p>${value.value}</p>`
}

div.innerHTML += dictionary

}


btn.addEventListener("click", storeValues);
Enter Word: <input type='text' class='newInput'/>
 <button type='submit' class='newInput'>Submit</button>
 
 
 <div class='dictionary'>
 <!-- Dicitonary will appear here-->
 </div>

Problem :

I’m trying to put every new word for title on the new line using js. I tried different ways, but they don’t work.

dictionary.title += word.value; – here I add attribute title for my dictionary class with word.value value. It comes together like: “HelloI’mJohn” instead of
“Hello
I’m
John”.
How can I do that on the new line using JavaScript code?

Comments

Comment posted by stackoverflow.com/questions/358874/…

At least related:

Comment posted by jsfiddle.net/tjcrowder/0b5cta7f

What is

Comment posted by T.J. Crowder

But the OP seems to be saying that they already have

Comment posted by Dmitry Bochok

It adds /n to the text, but doesn’t switch to the new line: “Hello/nI’m/nJohn”

By

Leave a Reply

Your email address will not be published. Required fields are marked *