Creating Filled DIVs using JavaScript

Although it may appear to be a common inquiry, my specific needs have not been addressed in any of the Stack threads I've come across.

I am dealing with a series of nested DIV statements, as demonstrated here (not all CSS included).

I am looking to create a basic script that can carry out the following:

for (i = 0; i < data.length(); i++) {
  //append DIV
  //add identifying class/id
  //change text values
}

If it helps, I am using d3.js for a few visualizations alongside these DIVS. If it would be easier, I can generate those DIVs in d3, but I'm unsure about how to go about it. Any advice or suggestions would be greatly appreciated. Thank you!

Answer №1

To generate multiple divs as children of another element, you can create new elements and then attach them to the DOM as children.

Suppose there is a main element on the webpage

<div id="myDiv">
    <p>This should be the starting point</p>
</div>

You can then add any number of elements using code like this:

for (a=0;a<3;a++) {
    var divTag = document.createElement("div"); 
    divTag.id = "div"+a; 
    divTag.setAttribute("align", "center"); 
    divTag.className = "myClass"; 
    divTag.innerHTML = a+" New div tag dynamically created."; 
    document.getElementById("myDiv").appendChild(divTag); 
}

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

Ways to resolve the issue of code not relocating the channel within a specific

The script I wrote to create a new category and channel produced an error message: TypeError: Cannot read property 'hasOwnProperty' of undefined. The issue occurs when the channel is not properly placed within the category. Here is the code snipp ...

Is it possible to retrieve event.data beyond the onMsg callback function in JS SSE?

My current project involves using three.js to display accelerometer data in real-time and in 3D on a web browser. To transfer data from the remote server to the client side, I am utilizing server-sent-events (SSE). While I have successfully implemented th ...

What benefits do Adobe Creative Cloud apps offer for developing interactive content in AEM?

My client recently transitioned to AEM for their website, and we are tasked with creating an interactive HTML5 'component' to be embedded on a page. While we have previously used custom HTML5 for components, integrating them into pages on the old ...

Despite functioning properly when triggered from the debug console, the jQuery AJAX request fails to work upon page load

The issue was resolved by disabling asynchronous requests in jQuery. Here is the Javascript & AJAX request (using jQuery) code on my page: "use strict"; var hsArea, counter, hotspots, count; counter = 4; count = 0; hotspots = {}; function fetchHots ...

The character replacement function does not seem to be functioning properly in DialogFlow following the use of

Is there a way to replace commas with colons in the 'setTime' result? I attempted to create another variable and then use the replace() method, but it resulted in an error message stating "Webhook call failed. Error: 500 Internal Server Error". ...

Save the HTML elements in an object and then use the ng-include directive to include the template

Currently experimenting with this code snippet: var CustomTable = function($elem) { this.properties.element = $elem; this.initialize(); }; CustomTable.prototype.PROPERTIE = { secondElement: $('.second') // this element ...

Tips for adjusting the font size on the Google Maps mapType controller

Is it possible to adjust the font size of a Google Maps mapType controller using HTML or CSS? I've managed to change the size of the controller, but struggling with the font size. https://i.sstatic.net/1n9oU.png HTML: <div #map id="map" style=" ...

Send PHP data through AJAX and retrieve it on a different PHP page

I need to send a variable through an AJAX URL to another PHP page and retrieve it using: $id=$_GET['id']; Here's an example where the value is passed, for instance $id=6. <a id="pageid" href="ifsc-bank.php?id=<?=$id?>"><im ...

The created hook does not execute when navigating to the same route with a different query parameters

One of my components has a created hook set up as follows: created() { if (this.$route.query.q) { //fetchdata } } But, when I try to change the URL within the same component using $router.push(`?q=${search}`), the URL updates but the creat ...

Changing the name of a React Native sample application

I am currently working on a project based on this App example: https://github.com/aksonov/react-native-router-flux/tree/master/Example When I tried to change the following lines: index.ios.js import App from './App'; AppRegistry.regist ...

Where do the props in React come from?

In my quest to learn React. I perused w3school's guide, but alas, the specific case I seek was not found. Even Google failed me in this pursuit, leading me here to pose my question. Behold, the code in question: <FooComponent foo="bar"> {( ...

Using NodeJS and EJS to Display MySQL Query Results

I am currently working on a project where I need to display data retrieved from a MySQL query in an HTML table. However, when I attempt to do so, I encounter the following output: [object Object],[object Object],[object Object],[object Object],[object Obj ...

Lazy loading Angular component without route - Form control name has no value accessor

Exploring lazyloading a component without routing. I have two components, each with its own formGroup. The parent component, named vehicleForm, includes a FormControl named vehicleDetail. The child component's formGroup includes fields for fuel and ...

Inserting data into a table using variables in Mssql database management system

I'm really struggling to find a way to safely add my Variables into an MSSQL server. I've tried everything. Could someone please help me and provide the solution for adding my Variables into the Database? It is crucial that I prevent any possib ...

While attempting to utilize inner class functions in Node JS, an error is being encountered

I've been delving into Node JS and exploring how to implement an OOP structure within node. I've created a simple class where I'm using functions to verify and create users in a database. However, I'm encountering a TypeError when attem ...

Removing a data entry from a PHP-generated MySQL table

In the process of developing a system, I have encountered a need to display database records in an HTML table. The functionality for creating the HTML table and retrieving data is working as expected. However, I am facing an issue with adding a column cont ...

Verify if an element with a specific index exists within an array

$.each(constructions, function(i,v) { if ($.inArray(v.name, map[ii].buildings) == -1) {//do something} }; In this scenario, the constructions array consists of unique objects with a name attribute. On the other hand, map[ii].buildings is an array contain ...

ReactJS component disappearing behind the Navbar

When using my React app, I have a navigation bar at the top. The Navbar component is called in App.js, and the code snippet below shows how it is implemented. export default function App() { return ( <Router> <Fragment> ...

Tips for styling an inline list using CSS before revealing it with jQuery's .show method:

In my project, I want to display a row of 'cells' which are simply images of black or white squares based on their ID. The issue I am facing is that when the button is clicked, the row list is shown without the CSS applied, and then after a brief ...

Guide to integrating react-phone-number-input into material-ui TextField

Would it be possible for me to use a Material UI TextField component as the inputComponent prop for the PhoneInput component from react-phone-number-input? I am facing an issue where I am unable to apply the ref successfully. Even though I can see the Mat ...