Use
n
or
br tag
for new line.
dictionary.title += word.value + "n";
Use
n
or
br tag
for new line.
dictionary.title += word.value + "n";
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>
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?
At least related:
What is
But the OP seems to be saying that they already have
It adds /n to the text, but doesn’t switch to the new line: “Hello/nI’m/nJohn”