Unable to add the BR element

After grabbing data from a textarea where each value is separated by a comma, I am transforming it into an array and adding each element to a div with the class name chars. Specifically, I want to ensure that only 5 children are appended per row within this div.

This is my JavaScript code:

var board = document.getElementsByClassName("chars")[0];
var input = document.getElementById("charInput").value; 

function appendElements(){
    
    input = input.replace(/ /g,'')
    var array= input.split(",");

    for(var i=0; i<array.length; i++){
        var elem = document.createElement("p");
        var textnode  = document.createTextNode(array[i]);
        elem.appendChild(textnode);
        board.appendChild(elem);     
    }
}

I'm not sure if CSS plays a role in resolving this issue. Here's the current CSS styling of my div (chars):

.chars{
    padding: 20px 20px;
    display: flex;
    justify-content: space-evenly;
}

I attempted the following but it did not achieve the desired result:

var br = document.createElement("br");
        if( (i+1)%5 ==0 ){
            board.appendChild(br);   
        }
        else{
            board.appendChild(elem);    
        }

Any help or advice would be greatly appreciated!

Answer №1

If you're looking to divide content into 5 columns, consider utilizing CSS Grid:

var board = document.getElementsByClassName("chars")[0];
var input = 'test,test,test,test,test,test,test,test,test,test,test,test,test,test,test,test,test,test'; 

(function splitArray()
{
    input = input.replace(/ /g,'')
    var array= input.split(",");

    for(var i=0; i<array.length; i++){
        var elem = document.createElement("p");
        var textnode  = document.createTextNode(array[i]);
        elem.appendChild(textnode);
        board.appendChild(elem);     
    }
})()
.chars {
    padding: 20px 20px;
    display: grid;
    grid-template-columns: auto auto auto auto auto;
}
<div class="chars"></div>

Answer №2

One way to enhance the layout of your child elements is by utilizing grid template with pure CSS.

display: grid;
grid-template-columns: repeat(4, 1fr);

To create spacing between the elements, simply add

grid-column-gap: 15px;

Keep in mind that this approach differs from using flex and may require adjusting for various screen sizes since it isn't the most responsive solution available.

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 determine if a string in JavaScript or jQuery ends with a specific

Is there a simple way to determine if a string concludes with a specific value? ...

Tips for placing a fixed footer at the bottom of a container with absolute positioning

I've set up my website container as a white box on a black background, centered on the page to allow for content resizing. Here is the code I used: .container { position: absolute; background-color: white; min-height: 90%; top: 5%; width: 9 ...

Utilizing WordPress Variables Beyond the Loop

Following the update of my loop to gather all necessary data using this code: <?php $json = ""; if ( have_posts() ) { while ( have_posts() ) { the_post(); global $wpdb; $query = "SELECT * FROM ". $wpdb-&g ...

Updating an image using AJAX and Flask

I have a situation in HTML where I need to constantly update an image file with the latest images that come in. Below is the Flask code snippet: @app.route('/uploads/update_file', methods=['GET', 'POST']) def update_file(): ...

Node.js Timer Functionality for Precision Timing

I'm currently in the process of developing a live chess application and one feature I'm looking to incorporate is a timer. The challenge I'm facing lies in ensuring the timer is accurate. After conducting various tests, I discovered that bot ...

Update the content of a div element by refreshing it using a click event

I'm having an issue with my page layout that includes 2 Div's. The first Div functions properly and displays a list of players. However, when you click on a link within that Div, it should load a second Div containing specific information about t ...

Showcase an image stored in my sqlite3 database on my Python Flask web application

My goal is to create a web application connected to a database where users can search for specific names and display the corresponding image stored in the database. The images in the database are stored as png BLOBs. The database is structured with a tabl ...

Clicking a button to reference a specific section within a list item

I'm currently working on developing a todo application using php, ajax, and mysql. I am facing a challenge where I need to implement a button that deletes an item from both the screen and the database. However, I am unsure about how to specify which i ...

The `process` variable is not recognized in a Vue/TypeScript component

I've encountered an issue trying to use .env variables in my Vue app. When I ran npm install process, the only syntax that didn't result in an error when importing was: import * as process from 'process'; Prior to this, I received the ...

Using Angular UI-Bootstrap typeahead within a table

Here's a straightforward question - how can I configure the typeahead feature to function in a table that is linked to a different table than the one my typeahead utilizes? For instance, suppose I have a foreign key in one table and I want users to b ...

Is it possible to hover over an image that incorporates a gradient effect?

My website features various communication icons, including one for Instagram that I sourced from Font Awesome. I matched the color of this icon to the official Instagram logo using a Gradient effect. However, I am encountering an issue where the hover effe ...

"Develop an interactive feature with jQuery that dynamically generates and removes fields according to the option selected from a

Can someone provide suggestions on how to achieve the functionality of adding and deleting input fields based on a user's selection from a drop-down menu using jQuery? I have successfully added the input fields, but I'm facing some confusion rega ...

Protractor encounters an error stating "No element found with specified locator" after attempting to switch to an

I've been attempting to download an embedded PDF from a webpage using Protractor selenium. However, I seem to be stuck when it comes to actually downloading the file as I always encounter the following error: Failed: No element found using locator: ...

Create a stunning design with Bootstrap 4 by incorporating a full-width background for both the table header and rows,

Bootstrap 4 offers a convenient table feature, but I'm having trouble aligning it with the design provided to me. The design requires full-width background colors for both the dark header (thead) and the light body (tbody), all while keeping the eleme ...

Using C language to pass a pointer of an array to a function in order to output the array contents

I'm currently working on a student management system and have encountered an issue with my output function. It seems to stop immediately when I call it, even though my input function works fine. I know that in C, you can't return a local array fr ...

The submit button seems to be unresponsive despite my inputting data into the form

I am having trouble with my code to request information from a whois server and display it on my webpage. When I press Enter, everything works fine, but the Submit button is not functioning properly. The current URL of the page is: ../whois.php After hi ...

Choose does not showcase the updated value

My form contains a form control for currency selection Each currency object has the properties {id: string; symbol: string}; Upon initialization, the currency select component loops through an array of currencies; After meeting a specific condition, I need ...

Ways to create space between columns in a table

How can I achieve the table layout with a gap between columns, as shown in the design? Initially, I tried using border-collapse: separate but found it difficult to adjust borders in the tbody. So, I switched to collapse. Is this the right approach for crea ...

Object.assign versus the assignment operator (i.e. =) when working with React components

Just a quick question: I've come across some answers like this one discussing the variances between Object.assign and the assignment operator (i.e. =) and grasp all the points made such as object copying versus address assignment. I'm trying to ...

Activating the option for selecting rows while also allowing for the selection of multiple cells

Recently, I discovered a feature in Angular-slickgrid that is not documented. When the enableExcelCopyBuffer flag is set to true, it allows for the selection of multiple cells. However, this functionality ceases to work when the enableRowSelection flag is ...