Ways to shuffle placement order in a CSS grid?

I'm completely new to coding and I'm attempting to create a bingo board that shuffles when a button is clicked using HTML, CSS, and Javascript. Right now, I have a CSS grid that arranges text div items in a 5x5 consecutive order. Is there a method to randomize the order of these div elements?

Example:

    <div class="grid-item"> 1 </div>
    <div class="grid-item"> 2 </div>
    <div class="grid-item"> 3 </div>
    <div class="grid-item"> 4 </div>
    <div class="grid-item"> 5 </div>

Answer №1

How about we try something like this?

<html>
<body>

<div id = "bingo">
</div>

<button onclick = "shuffleBoard()">Shuffle</button>

<script>
    createBoard();

    function createBoard() {
        var str = "";
        for (var i = 1; i <= 25; i++) {
            str += "<div class = 'grid-item'>" + i + "</div>";
        }
        document.getElementById("bingo").innerHTML = str;
    }

    function shuffleBoard() {
        var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25];
        var str = "";
        while (numbers.length > 0) {
            var i = Math.floor(Math.random() * numbers.length);
            str += "<div class = 'grid-item'>" + numbers[i] + "</div>";
            numbers.splice(i, 1);
        }
        document.getElementById("bingo").innerHTML = str;
    }
</script>

</body>
</html>

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

Using jQuery to locate the href attribute within a TD element with a specific

Update URL <td class="posts column-posts"><a href="edit.php?tshowcase-categories=ops&amp;post_type=tshowcase">2</a></td> Current URL: <span class="view"><a href="https://blog.company.com/team/tshowcase-categories/ops ...

In Vue.js, you can pass variables from a row as parameters to a Vue.js method and then send them out using axios

I am working on a web app that uses Django for the backend and Vue.js for the frontend. Each row in a table has a button that is intended to display detailed information about that specific row when clicked. I am trying to pass certain variables from a row ...

What is the best way to run a series of basic commands in Node.js in sequence

Is there a way to execute 4 bash commands sequentially in nodejs? set +o history sed -i 's/&& !this.peekStartsWith('\/\/')/ /g' dist/vendor.bundle.js sed -i 's/&& !this.peekStartsWith('\/\/ ...

Encountering a problem with jQuery selectable() that causes the selections to cycle through their history before being successfully posted

My issue involves a table where users can select multiple rows, each with a unique ID. These selected IDs are then aggregated into a string and sent via POST to a php file. Everything works perfectly fine, except for one problem. When rows are continuously ...

Automatic updates are enabled for Google Chrome extensions

I noticed that all the extensions I analyzed had a specific code in their manifest.json, even though Google says you don't have to include it if you host your Chrome app on their servers. Should I also include this code in my Chrome extension manifest ...

Prevent row expansion when using Flexbox for expandable items

I am facing an issue with my flexbox layout where the items, which are expandable like dropdowns, expand along with the row when clicked. I want only the item to expand without affecting the rest of the row. Current behavior: https://i.sstatic.net/60wBh.p ...

Establish a connection to a designated route in Node.js

I have a specific path that I want to establish a socket connection to: http://localhost:3000/home/. Below is my index.js code: var express = require('express'); var app = express(); var http = require('http').Server(app); var path = r ...

Join the geomarkers in two datasets using connecting lines

I am currently developing a leaflet.js map incorporating two distinct sets of JSON data layers stored as js-files. The first dataset comprises the geographical locations of various newsrooms along with their respective IDs, while the second dataset contai ...

Issue with clearing input field after submitting form (React)

I'm struggling to get the input field to clear automatically after clicking the submit button in my React component. I've tried making it fully controlled by React, where the input value depends solely on the state, but I can't seem to figur ...

Is there a way to utilize Material UI React Grid, which incorporates CSS Flexbox, in order to achieve a specific grid layout where items are wrapped and evenly spaced?

Currently, in my React project, I am incorporating Material UI React. I am working with the <Grid> component to create a flexbox layout with 10 items. I am facing some difficulties in achieving a perfect grid layout with evenly spaced items. The lay ...

Incorporating Jest alongside setTimeout and leveraging useFakeTimers

As I work with a TypeScript async function that requires a 1-second sleep between two statements, this implementation is in place: async function systemUnderTest(): Promise<void> { console.log("One"); await new Promise(r => { set ...

The length of the array does not correspond to the index position

var index = 0; var length = ["a", "b", "c", "d", "e"].length; var interval = setInterval(function() { if (index < length) { console.log(index); console.log(length); index++; } else { clearInterval(interval); } }, 1000); Is there a solution t ...

The CSS ID selector fails to load on Android WebView

I am facing an issue while trying to load HTML with CSS that includes an ID selector. The file is not loading to the webview as expected. Here is the code snippet I attempted to load: <!DOCTYPE html> <html> <head> ...

Create a website that is both reorderable, flexible, and resizable

I want to create a similar design using jquery/html/css. Can anyone provide an article or explanation on how to get started making something like this? ...

Toggle the submit button using jQuery based on the comparison of the jml_automatic field with the id_km field

I recently learned jQuery last month, and now I am working on a form using jQuery to allow users to input gas requests. However, the script is not working to compare the values from #jml_km with #id_km, and the submit button is still not disabled when the ...

Showing the Nested Object following retrieval from the API

Greetings everyone, I am facing an issue with displaying basic data from an API service that contains a NESTED json object. The challenge I am encountering is that most tutorials only focus on displaying data from array objects, not nested ones. The str ...

"Prior to executing the sort function, the JavaScript array undergoes a

Update: It seems like the issue is related to how the chrome console handles object evaluation, as pointed out by jsN00b. original source I am currently sorting an array by name and then by age, and logging the array three times: First right after initia ...

The cropper fails to load within the image element inside a modal pop-up

Utilizing fengyuanchen's cropper library, I am cropping an uploaded image and then submitting it to a form. <div id="change-dp" uk-modal> <div class="uk-modal-dialog uk-modal-body"> <button class="uk ...

What is causing the failure in retrieving the smallest number from this array?

The maximum value should be 9.99, and the minimum value should be 6.88 let arr = [["2019","00","01", 9.99], ["2018","00","01", 9.32], ["2017","00","01", 6.88]] let max = Math.max(Number(...arr.map((o) => { return o[3] }))); //9.99 let min = Math.mi ...

Spring Ajax response returns empty objects in the list

I'm currently developing a locker management system which includes an update history feature for the lockers. I am interested in implementing Ajax to retrieve a list of this update history, limited to X entries. However, when I return either List<L ...