If you want to include spaces between text to move to the next line in JavaScript, you need to use a line break character or a combination of a line break and spaces. There are different ways to achieve this:
Using Line Breaks:
You can use the newline character (\n) to move to the next line. For example:
var multiLineText = "Hello\nWorld"; console.log(multiLineText); // Output: // Hello // World |
Using Template Literals:
Template literals allow you to include line breaks directly within the string using backticks:
var multiLineText = `Hello World`; console.log(multiLineText); // Output: // Hello // World |
Using String concentration:
You can use string concatenation to add line breaks between strings:
var multiLineText = "Hello" + "\n" + "World"; console.log(multiLineText); // Output: // Hello // World |
Let’s understand this with an example:
Presently, Our output is looking like this
After doing some minor changes in our javascript code:
Javascript code:
// Get the submit-code button element var submitCodeButton = document.getElementById("submit-code"); // Get the next element after the submit-code button var nextElement = submitCodeButton.nextElementSibling; // Add a click event listener to the submit-code button submitCodeButton.addEventListener("click", function() { // Get the textarea element and set its value to the text content of pre code document.querySelector("pre code").textContent = document.querySelector("textarea").value; }); // Add a click event listener to the next element after the submit-code button nextElement.addEventListener("click", function() { // Get the textarea element and set its value to the text content of pre code document.querySelector("textarea").value = document.querySelector("pre code").textContent; }); |
pre { white-space: pre; } textarea { height: 240px !important; } p { font-weight: bold; } |
Top QuestionsRelated QuestionsHow to display the list of items corectly and add a scrolling text slider?
![]() Asked 10-01-2024Ritu Singh
How to include spaces between text to enter the consecutive text in next line in javascript?
![]() Asked 16-01-2024Ritu Singh
How to replace with hide/show div1 with div2 using onclick() and a function in JavaScript?
![]() Asked 10-01-2024Ritu Singh
|