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:

To illustrate, here is the desired outcome:

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

Version 1 of Vue.js is not compatible with Ajax-loaded HTML files

Currently, I am encountering a minor issue with loading content through ajax requests. I am in the process of developing a web application where everything is located on one page without any reloading. To achieve this, I have split the content into separa ...

Incorporating a Registration Popup Form in ASP.NET

Looking to implement an Ajax popup for a registration form on my ASP.NET page. What is the recommended approach to achieve this? How can I ensure that my database is updated without needing to refresh the page? ...

Is there a way to ensure that an external file function completes before proceeding with the main code execution?

In my project, I have two key JavaScript files: main.js and load.js. Within load.js, there are two essential functions - getData, which makes an AJAX post call, and constHTML, which appends data based on the JSON array returned from the AJAX call. This app ...

Organizing your thoughts: Utilizing Etherpad-Lite's list

Looking to stylize the list items with decimal, upper-alpha, lower-alpha, upper-roman, and lower-roman for each of the first five levels? I attempted to achieve this by adding CSS in my pad.css file. .list-number1 li:before { content: counter(first)") " ...

Odd behavior of the "for in" loop in Node.js

It seems like I'm struggling with the use of the "for in" statement. When working with a JSON document retrieved from a mongodb query (using nodejs + mongoose), its structure looks something like this: [{ "_id":"596f2f2ffbf8ab12bc8e5ee7", "da ...

Words fail to display

.sport { content: url("../img/sport.png"); background-color: rgba(0,0,0,1.0); top: 61px; height: 310px; width: 300px; position: absolute; margin: 0; left: 0; border-radius: 4px; overflow: hidden; } .sportsandactivis { background-colo ...

Is there a way to set a default value for the map function?

Currently utilizing React.js with the following code snippet: myArray.map(variable=>({value: variable.value, label: variable.label})) It's working well for the most part, but occasionally encountering this issue: TypeError : myArray is null Any s ...

Cloning a file input does not retain the uploaded file in the cloned input. Only the original input retains the uploaded file

Whenever I duplicate an input type file, I encounter an issue where the uploaded file from the duplicated input is also linked to the original input. It seems like the duplicate input is somehow connected to and taking files for the original one. This is ...

How can I center two divs within a wrapper div without also centering all the text inside?

Is there a method other than using display: inline-block and text-align:center that allows us to center two divs inside a wrapper div? I prefer not to have my text centered. ...

Tips for crafting paragraphs that double as sieves

I'm trying to simplify and shorten this code. I have three HTML paragraphs acting as filters: "all", "positive," and "negative", referring to reviews. Each has a corresponding div for reviews: "allcont", "poscont", and "negcont". Clicking on any of th ...

What is required to create a basic application that can function offline while featuring an HTML/CSS user interface?

Newbie inquiry: I am interested in creating a small application that can run offline on a desktop computer. The amount of data to be saved is minimal, so I have the option to use a file or some type of database. However, my main question is: What languag ...

What are the techniques for integrating PHP code into JavaScript/Ajax?

I am curious about how to integrate PHP code into JavaScript/Ajax. Here is the PHP code I am working with: if ($folder = opendir('data/Tasklist/')) { while (false !== ($file = readdir($folder))) { if ($file != '.' && $ ...

Is it possible to render the v-for value dynamically?

I have a dynamic component and I'm trying to iterate through different objects from it. However, my current code isn't working as intended. Can someone please guide me on how to make the activeQuestion value in v-for dynamic? Thank you for your a ...

Integrating Watson Conversation with Oracle Database

Hello everyone, I am currently working on a project where I need Watson to fetch a response manually set from our Oracle Databases. To achieve this, I am using async to access the database sequentially and return the response. Initially, I faced an issue ...

Unable to group the array based on the key value using Jquery or Javascript

I am looking to group my array values based on specific key values using Jquery/Javascript, but I am facing issues with my current code. Let me explain the code below. var dataArr=[ { "login_id":"9937229853", "alloc ...

Tips for refreshing the page without losing the values of variables

In my simulation.jsp file, I have the following code that retrieves simulation data from a struts2 action: $(document).ready(function() { var data='<s:property escape="false" value="simInfos" />'; } Once I perform the simulation with this ...

Utilizing distinct useState for mapped elements

I am struggling to find a solution on how to pass specific useState states to respective mapped elements. export const Polska = () => { const [riverVisible, setRiverVisible] = useState(false) const [mountainVisible, setMountainVisible] = useState(fa ...

How to Test React Js API Calls with Jest?

I'm currently in the process of writing test cases for my API call function, but I'm encountering difficulties as I am unable to successfully run my tests. Below is the code for the API call function and the corresponding test cases. export async ...

Converting an object with a combination of different index types into a string representation

I'm dealing with a unique object structure that behaves similarly to an array, except it contains a mix of index types (numbers and strings). Here's an example: var myObj = []; myObj[0] = 'a'; myObj[1] = 'b'; myObj[2] = &apos ...

Steps for creating a square button

Is it possible to create a square button on my website that still functions like a traditional button? I want the button to change appearance slightly when scrolled over. Currently, I have an example where the button is not clickable: http://jsfiddle.net ...