make 2xn grid in bootstrap in javascript

To create a 2xn grid in Bootstrap using JavaScript, you can first create a container element for the grid and add it to your HTML document. Then, using JavaScript, you can create and append the necessary row and column elements to create the grid.

Here is an example code snippet:

<div id="grid-container" class="container"></div>
50 chars
2 lines
index.tsx
// get the container element
const container = document.getElementById("grid-container");

// set the number of columns you want to create
const numCols = 2;

// set the number of rows based on the number of items you want to display
const numRows = Math.ceil(items.length / numCols);

// loop through each row and column to create and append the necessary elements
for (let i = 0; i < numRows; i++) {
  // create a new row element
  const row = document.createElement("div");
  row.classList.add("row");

  // loop through each column in the row
  for (let j = 0; j < numCols; j++) {
    // determine the index of the item to display in this column
    const index = i * numCols + j;

    // create a new column element
    const col = document.createElement("div");
    col.classList.add("col");

    // check if there is an item to display in this column
    if (index < items.length) {
      // create a new item element
      const item = document.createElement("div");
      item.classList.add("item");
      item.innerHTML = items[index];

      // append the item to the column
      col.appendChild(item);
    }

    // append the column to the row
    row.appendChild(col);
  }

  // append the row to the container
  container.appendChild(row);
}
1258 chars
43 lines

In the above code, the items variable is an array of data to be displayed in the grid. You can replace this with your own data or remove it if you want to add data programmatically. The classes used (row, col, and item) are Bootstrap classes for creating a grid layout. You can modify these as needed to match your own layout requirements.

gistlibby LogSnag