Tips for adding line breaks once html tags containing textContent are obtained

This is the image of my current task.

https://i.sstatic.net/Ze2Oc.png

I managed to retrieve HTML tags using

document.getElementById("code").textContent = document.getElementById("code-output").innerHTML;

The content within "code-output" spans from the header above to just before the rectangle box. The "code" ID corresponds to the rectangle box where I intend to showcase the aforementioned HTML tags.

My objective is to create a new line after each HTML tag (lower box). Is there a method to accomplish this?

Thank you.

Here is the code snippet:

document.getElementById("code").textContent = document.getElementById("code-output").innerHTML;
#container {
  width: 80%;
  margin: 0 auto;
  text-align: justify;
}

#code {
  display: block;
  height: auto;
  overflow-y: scroll;
  font-family: "Courier";
  font-size: 18px;
  padding: 20px;
  border: 1px solid #DCDCDC;
}
<div id="container">
  <div id="code-output">
    <h1> Sample Heading </h1>
    <p>
      ...
    </p>
    <p>...
    </p>
    <h2>Lorem Ipsum dolor sit amet, consectetur adipiscing elit.</h2>
    <p>
      ...
    </p>
  </div>
  <!-- code output -->

  <code id="code">
  </code>
</div>
<!-- end of container -->

Answer №1

Make sure to include the following at the conclusion of your script:

+ "\n"

For example:

document.getElementById("script").textContent = document.getElementById("output-code").innerHTML + "\n"

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Function to save prices as cent-based figures in Javascript using Regex

Trying to extract prices from a string using regex can be tricky, as unexpected issues may arise. For example, obtaining the following values: US$1234.56 $12 $12.34usd $0.56 .56 dollars and converting them to: 123456 1200 1234 56 56 is necessary for s ...

When creating a new instance of the Date object in Javascript, the constructor will output a date that is

In my project using TypeScript (Angular 5), I encountered the following scenario: let date = new Date(2018, 8, 17, 14, 0); The expected output should be "Fri Aug 17 2018 14:00:00 GMT-0400 (Eastern Daylight Time)", but instead, it is returning: Mon Sep ...

Tips for maintaining the position of a camera in three.js while also keeping its rotation fixed on the origin

In three.js, I'm looking to dynamically adjust my camera's position while ensuring that its rotation automatically aligns with the world origin. For instance, if the camera is initially set at: camera.position.set(25,25,25) I aim to have the ...

Combining JSON data in Typescript using the merge method

Is there a way to effectively merge JSON objects in such a manner that simple values (strings, numbers, booleans, etc) get overridden when keys match, but when dealing with complex values (arrays and objects), different outcomes apply? 1.) For simple array ...

Different width can be set for the Google Maps template specifically for mobile mode by following these steps

Is there a way to adjust the width of a Google Maps template specifically for mobile mode? While it looks perfect on desktop, the map overflows the screen size on mobile. See my code snippet below: @section ('map') <div id="map"></d ...

Background image spacing

Can anyone explain why there is extra space after my background image in this code snippet? (red line in image highlights the spacing issue) Even though the background image doesn't have this space, it seems to extend beyond where the black color ends ...

Struggling with implementing CSS in React even after elevating specificity levels

I am struggling with a piece of code in my component that goes like this: return ( <div className="Home" id="Home"> <Customnav color="" height="80px" padding="5vh"/> <div className= ...

Transforming the Looping Data in the GetJson Array into a Clickable Link

I am facing an issue with creating a list of markers on my Google Maps. I want to make a link that will display the values of each marker on the list, as it is looped through using getJson and <li> is only appended during the loop. Could someone ass ...

What exactly does ngDefaultControl mean within the framework of Angular?

Hey there! This question is not a duplicate, I assure you. The issue at hand involves adding a certain directive to a tag that already contains the [(ngModel)] directive but is not nested within a form element. Without this additional directive, an error p ...

Determine the percentage of font size in text and apply it to the title

Is there a way to pass on font-size from one element to another using a combination of functions? For example, if my 'p' font-size is set at 90% and I want my new font size to be 1.5 times larger than that, which would be 135%, is it possible to ...

Is it possible to establish borders in relation to the text?

Take a look at this image that illustrates my goal. What I want to achieve is having lines on both sides of a text element (usually a heading tag) that stretch out a specific distance (in this case, 65px). I'm looking for a solution that adjusts dyn ...

Populate Chart.js with a specific set of JSON data

As I delve into the world of JavaScript, I'm faced with a challenging issue that I need help resolving: I have a JSON file housing an array of data. What I aim to do is extract specific arrays from the month of August (Reports, Average, Global) and d ...

Having trouble with my Angular application, seems to be stuck at the loading screen. Not sure what's causing it, possibly

Hey there, I'm hoping to create a straightforward app that showcases posts. However, I've encountered an issue when deploying the initial page which is stuck on "Loading...". I believe it's a minor problem and would appreciate your assistan ...

Display the specified div element automatically

Despite my best efforts, I can't seem to figure this out. I've searched everywhere for a solution but no luck so far. Is there a way to automatically display the content from http://tympanus.net/Tutorials/CSS3ContentNavigator/index.html#slide-mai ...

What is the method for incorporating a currency symbol into a jQuery mask?

I have successfully implemented the mask on the <td> element of the table, but now I would like to include the currency symbol before the value as shown in the example below. if ($(this).attr('title')==='Value') { $(+ ...

Encountered an Angular SSR error stating "ReferenceError: Swiper is not defined"

When attempting to implement SSR (Server-Side Rendering) in a new project, everything runs smoothly and without issue. However, encountering an error arises when trying to integrate SSR into an existing project. https://i.sstatic.net/QOI6A.png ...

Encountering a glitch when utilizing framer-motion's Animated Presence feature

I am working on a React slider where new data replaces old data whenever the user clicks on the Next or Previous button. To enhance the slider with beautiful animations, I decided to use framer motion. The animations are almost perfect, but for some reason ...

What is the best way to make a background image that adjusts in size while maintaining the aspect ratio?

Looking to set up a unique loading screen for my GMOD server. When players are using a 4:3 monitor, the background image needs to be resized or cropped accordingly. Any suggestions on how I can achieve this? ...

What steps are involved in launching an outdated Angular project?

Tasked with reviving an old Angular client in my company, I found myself grappling with outdated files and missing configurations. The lack of package.json, package-lock.json, and angular.json added to the confusion, while the presence of node modules in t ...

Regular expressions are used for validation purposes

I've been using the regular expression shown below to validate certain text: ^\d+(?:fs|sf)[-+]\d+[hmd]$/ Here are some sample texts I have validated with this regular expression: 20fs-4d 10sf+20m 3fs-2h So far, it's been working we ...