I'm attempting to insert a line break after HTML elements that are being added from JavaScript code inside a `display: flex` div

I'm facing an issue where line breaks are not added after HTML elements are inserted via JavaScript upon clicking a button.

For instance, the data from the inputs doesn't get separated even when I click submit multiple times:

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

To illustrate, here is the desired outcome: https://i.sstatic.net/IcjYg.png

Below is the function code that executes after the button is pressed:

    function addData() {
    // saving input values
    // displaying calculations and values on screen while also storing them locally
    let strategyValue, sharesValue, enterPriceValue, stopLossValue, profitPriceValue, stockNameValue;

    // creating html nodes
    let breakLine = document.createElement("br");
    let stockStrategy = document.createElement("h2");
    let stockValue = document.createElement("h2");
    let stockPrice = document.createElement("h2");
    let stockStopLoss = document.createElement("h2");
    let stockProfitPrice = document.createElement("h2");
    let stockName = document.createElement("h2");


    // appending the html nodes to div-result in the DOM for flex display
    
    

    // saving input values to variables
    strategyValue = inputStrategy.value;
    sharesValue = inputShares.value;
    enterPriceValue = inputEnterPrice.value;
    stopLossValue = inputStopLoss.value;
    profitPriceValue = inputProfitPrice.value;
    stockNameValue = inputStockName.value;

    // assigning values to the html nodes 
    stockStrategy.innerHTML = strategyValue;
    stockValue.innerHTML = sharesValue;
    stockPrice.innerHTML = enterPriceValue;
    stockStopLoss.innerHTML = stopLossValue;
    stockProfitPrice.innerHTML = profitPriceValue;
    stockName.innerHTML = stockNameValue;

    // adding the stockTitle to div-result in the html node

    document.body.appendChild(stockName);
    document.body.appendChild(stockStrategy);
    document.body.appendChild(stockValue);
    document.body.appendChild(stockPrice);
    document.body.appendChild(stockStopLoss);
    document.body.appendChild(stockProfitPrice);
    


    divResult.appendChild(stockStrategy);
    divResult.appendChild(stockValue);
    divResult.appendChild(stockPrice);
    divResult.appendChild(stockStopLoss);
    divResult.appendChild(stockProfitPrice);
    divResult.appendChild(stockName);
    
}

CSS Code:

    .div-result {
    display: flex;
    justify-content: center;
    border: solid 5px blue;
}

.div-result h2{
    color: blue;
    margin-left: 5%;
}

HTML Code:

    <body>
    <div class="title">
        <h1>Power Tracker</h1>
    </div>

    <div class="div-main-inputs">
        <div class="div-inputs">
            <label>Stock Name</label>
            <input value="" type="text" class="input-stock">
            <label>Choice Strategy</label>
            <input value="" type="text" class="input-strategy">
            
            
        </div>

        <div class="div-secondInputs">
            <label>Bought Price</label>
            <input value="" type="number" class="input-enter">
            <label>Stop Loss Price</label>
            <input value="" type="number" class="input-stop-loss">
            <label>Profit Price</label>
            <input value="" type="number" class="input-profit-price">
            <label>Choice Shares</label>
            <input value="" type="number" class="input-shares">
        </div>

        <button class="btn-submit">Submit</button>

    </div>
    <div class="div-result">


    </div>


    <script src="script.js"></script>
</body>

Appreciate any assistance provided.

Answer №1

It appears that there are several aspects you may not fully comprehend. If you have specific inquiries regarding the solution, feel free to inquire. Before proceeding, please carefully review the provided solution.

function addData() {

    // Storing values from input fields
    // Displaying calculation results and saving them in local storage
    let strategyValue, sharesValue, enterPriceValue, stopLossValue, profitPriceValue, stockNameValue;

    // Creating HTML elements
    let breakLine = document.createElement("br");
    let stockStrategy = document.createElement("h2");
    let stockValue = document.createElement("h2");
    let stockPrice = document.createElement("h2");
    let stockStopLoss = document.createElement("h2");
    let stockProfitPrice = document.createElement("h2");
    let stockName = document.createElement("h2");

    // Appending HTML elements inside div-result to display them flexibly

    // Saving input values into variables
    strategyValue = document.getElementById('strategy').value;
    sharesValue = document.getElementById('strategy').value;
    enterPriceValue = document.getElementById('strategy').value;
    stopLossValue = document.getElementById('strategy').value;
    profitPriceValue = document.getElementById('strategy').value;
    stockNameValue = document.getElementById('strategy').value;

    // Assigning values to the HTML elements
    stockStrategy.innerHTML = strategyValue;
    stockValue.innerHTML = sharesValue;
    stockPrice.innerHTML = enterPriceValue;
    stockStopLoss.innerHTML = stopLossValue;
    stockProfitPrice.innerHTML = profitPriceValue;
    stockName.innerHTML = stockNameValue;

    // Adding the stockTitle to div-result in the HTML node
    divResult = document.getElementById('divResult');
    
    let div = document.createElement("div");

    div.className='div-result'

    div.appendChild(stockName);
    div.appendChild(stockStrategy);
    div.appendChild(stockValue);
    div.appendChild(stockPrice);
    div.appendChild(stockStopLoss);
    div.appendChild(stockProfitPrice);
    div.appendChild(breakLine);
   
   divResult.append(div)
    
}
.div-result {
    display: flex;
    justify-content: center;
}

.div-result h2{
    color: blue;
    margin-left: 5%;
}

.div-result0{
    border: solid 3px blue;
}
<body>
    <div class="title">
        <h1>Power Tracker</h1>
    </div>

    <div class="div-main-inputs">
        <div class="div-inputs">
            <label>Stock Name</label>
            <input value="" type="text" class="input-stock" id='stockName'>
            <label>Choice Strategy</label>
            <input value="" type="text" class="input-strategy" id='strategy'>
            
            
        </div>

        <div class="div-secondInputs">
            <label>Bought Price</label>
            <input value="" type="number" class="input-enter" id='enterPrice'>
            <label>Stop Loss Price</label>
            <input value="" type="number" class="input-stop-loss" id='stopLoss'>
            <label>Profit Price</label>
            <input value="" type="number" class="input-profit-price" id='profitPrice'>
            <label>Choice Shares</label>
            <input value="" type="number" class="input-shares" id='inputShares'>
        </div>

        <button class="btn-submit" onclick="addData()" >Submit</button>

    </div>
    <div class="div-result0" id='divResult'>


    </div>


    <script src="script.js"></script>
</body>

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

What causes jquery height and javascript height to both be returned as 0?

I'm facing an issue with a visible div on my screen - despite being visible, its height always returns as 0. I've attempted various jQuery and JavaScript methods to retrieve the height, but it consistently shows as 0. Here's the structure of ...

Using both withNextIntl and withPlaiceholder simultaneously in a NextJS project causes compatibility issues

I recently upgraded to NextJS 14 and encountered an issue when deploying my project on Vercel. The next.config.mjs file in which I wrapped my nextConfig in two plugins seemed to prevent the build from completing successfully. As a workaround, I decided t ...

Learn how to dynamically highlight a table row when any changes are made to it using jQuery

Is there a way to automatically highlight the current table row when any input text or image is changed within that specific row using jQuery? In the given code snippet below, with two rows provided, how can one of the rows be highlighted upon modifying ...

Exploring through an array tree structure?

My menu displays the "selected" array as options. When an option is selected, its branches are rendered as new options. To keep track of the traversal, I create an array called select. For example, if someone selects the 3rd option, then the 1st option, a ...

Is there a way to extract the HTMLElement[] type from the DOM?

In my TypeScript project, I am trying to gather all the top-level elements of a page using the code snippet below: const getHTMLElement() : HTMLElement[] { const doc = document.body.children; const list : HTMLElement[] = []; for (let c of Array.f ...

What is the best way to arrange elements above and below each other in a single flexbox container?

Currently, I am facing a challenge aligning the number 238,741 and the letter N in Number. There is a padding between the Prize Pool div and the Number Active div. All of these elements are enclosed within a parent container with display set to flex and fl ...

Toggling with Jquery when an image is clicked

I'm trying to wrap my head around the functionality of jquery toggle. My goal is to toggle to the next anchor element with the class plr-anchor when an image with the class go_down is clicked. The information is being populated using maps. Javascript ...

How can I add color to arrow icons in tabulator group rows?

Is there a way to specify the color of the arrows in collapsed/expanded group rows? Also, what CSS code should I use to define the font color for column group summaries? You can view an example of what I am trying to customize here: https://jsfiddle.net/s ...

Executing a JavaScript/jQuery function on the following page

I'm currently working on developing an internal jobs management workflow and I'd like to enhance the user experience by triggering a JavaScript function when redirecting them to a new page after submitting a form. At the moment, I am adding the ...

Change the <base> element programmatically during server-side rendering

Searching for a way to obtain the base URL for an HTML page, so that relative URL requests from the browser utilize that base. If you are looking for answers, check out this link: Defining root of HTML in a folder within the site root folder When serving ...

How to enhance and expand Material-UI components

I am trying to enhance the Tab component using ES6 class in this way: import React from "react"; import {Tab} from "material-ui"; class CustomTab extends Tab { constructor(props){ super(props); } render(){ return super.rende ...

What is the best way to add a darker tint to the background behind my overlay panel in jQuery

After grabbing the panel code from the jQuery Mobile website and integrating it into my file, I am interested in changing the appearance. I want to either darken or blur the background content when the panel is displayed. I attempted to use filter:blur(5px ...

What could be causing JavaScript's wrapInner function to throw an error?

Somehow, the Firefox add-on I've been working on isn't functioning as expected. The initial line of code is executing without issues, which wraps content in a div element with an id of 'content_cont'. However, any subsequent lines of co ...

Close any open alerts using Protractor

While using protractor and cucumber, I have encountered an issue where some tests may result in displaying an alert box. In order to handle this, I want to check for the presence of an alert box at the start of each test and close/dismiss it if it exists. ...

How can I resolve the issue of using string values for items inside v-autocomplete, but needing them to be numbers in v-model?

I am working with a v-autocomplete component <v-autocomplete v-model="addressRegion" :items="selectLists.regions" item-value="key" item-text="value" ></v-autocomplete> The AddressRegion is curren ...

Idiosyncratic TypeScript behavior: Extending a class with comparable namespace structure

Lately, I've been working on creating a type library for a JavaScript written library. As I was defining all the namespaces, classes, and interfaces, I encountered an error TS2417 with some of the classes. I double-checked for any issues with method o ...

Retrieve a Vue Component by utilizing a personalized rendering method for a Contentful embedded entry

Currently experimenting with Contentful, encountering some issues with the Rich text content field. To customize the rendering of this block and incorporate assets and linked references in Rich text content, I am utilizing the modules '@contentful/ri ...

Is there a way to integrate PHP functionality into JavaScript?

When it comes to using JavaScript and PHP together, a common challenge is retrieving information from a database and utilizing that data in a JavaScript function. In my case, I need this functionality for implementing password change functionality on a w ...

What is the best way to incorporate arrowheads into the lines that have been sketched using canvas

I need assistance with incorporating arrowheads at the end of linear plots drawn on a coordinate grid using my custom function. You can view an example of the current setup in this JsFiddle: https://jsfiddle.net/zje14n92/1/ Below is the code snippet respo ...

Having trouble with understanding the usage of "this" in nodejs/js when using it after a callback function within setTimeout

It's quite peculiar. Here is the code snippet that I am having trouble with: var client = { init: function () { this.connect(); return this; }, connect: function () { var clientObj = this; this.socket = ...