Sorting div elements by class name using a for loop in Javascript

I just started learning Javascript and I managed to create square boxes with SVG dots on top of them.

Now, I'm looking to number these divs using a for loop by storing them in an array based on their div id. Can anyone provide guidance on how to correct my code?


    //Javascript
    //create twelve dotBoxes evenly spread across the window
    function getGridAreaSize(){
    $('#grid-area').width($(window).width() * .5);
    }

    function getBoxSize(){
    var boxWidth = $('#grid-area').width() / 5;
    return boxWidth;
    };

    function createBoxes(){
    for (i = 0; i < 15; i++) {
    // divsToAppend += '<div id="div' + (++i) + '" />';
            // arrayDiv[i] = document.createElement('div');
            // arrayDiv[i].id = 'dot' + i;
     // arrayDiv[i].className = 'dotBox';
     //             document.getElementById('#grid').appendChild(arrayDiv[i].id);
        // document.getElementById(arrayDiv[i].id).appendChild(dotarea);
        // document.getElementById('#dotarea').appendChild(dot);
$('#grid').append("<div class='dotBox'><div class='dotarea'><div class='dot'></div></div></div>");
$('.dotBox').css('width',getBoxSize());
$('.dotBox').css('height',getBoxSize());
};
    };

    getGridAreaSize();
    getBoxSize();
    createBoxes();

    //html
    <!DOCTYPE html>
    <html>

//Rest of HTML content removed for conciseness

<script src="scripts/dots.js"></script>
</body>

</html>

I have been trying to display different text when hovering over each box but my attempt to store them in an array has not worked as expected. Any suggestions?

Answer №1

Include the 'i' attribute when adding it to the div:

$('#container').append("<div class='circleBox' id='circle_"+i+"'><div class='circleArea'><div class='circle'></div></div></div>");

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

Encountering a problem with the Link tag in react-router-dom

I'm currently tackling a React challenge that requires me to integrate a Link tag from react router with some text. While the link works perfectly in the browser, I am unable to pass the test as it keeps throwing an error at me. I have exhausted all p ...

Updating existing records in a Waterline Many-To-Many relationship

I am currently facing a challenge with updating my model in order to allow my application to send an array of Id's (or object Id's) to the server for the update function to replace all existing relations with the new ones. Here is an example of ...

A guide on using jCrop to resize images to maintain aspect ratio

Utilizing Jcrop to resize an image with a 1:1 aspect ratio has been mostly successful, but I've encountered issues when the image is wider. In these cases, I'm unable to select the entire image. How can I ensure that I am able to select the whole ...

Produce a variety of shapes using Three.js

I am looking to generate multiple objects. var distance = 10; var geometry = new THREE.BoxGeometry(10,10,10); var material = new THREE.MeshBasicMaterial({color:0x00ff44}); for(var i = 0; i < 4;i++){ var mesh = new THREE.Mesh(g ...

Rendering data in React using the array indexRendering data in React

I'm encountering an issue in React JS where I need to display all data from a REST API with a numeric index. REST API: [ { "id": "1", "start_date": "2020-05-08 09:45:00", "end_date": "2020-05-08 10:00:00", "full_name": "mirza", "cust_full_name": "fu ...

Converting a JSON object into an array of objects

I am looking to transform the following JSON structure let data = { item1: [11, 12, 13, 14, 15], item2: [16, 17, 18, 19, 20] } into this specific format using JavaScript's native functionalities of arrays or objects (compatible with all web brow ...

Trigger a child-mounted event and retrieve it from the parent component

Imagine I have a component named child. There is some data stored there that I need to retrieve in the parent component. To accomplish this, I plan to emit an event in the childs mount using this.$emit('get-data', this.data), and then receive it ...

I am looking to download a file from a server and showcase it in a browser using React.js. The file will be received as a response from the

**I am looking to retrieve a file from the server by sending the file name through the body and then receiving the requested file from the server.** I have successfully received the file in response from the server, which I tested using Postman. What I a ...

Getting dynamic props from a clicked element in React involves accessing the target element's properties and

I am currently working with a React "tree" menu component that has main links with submenus generated dynamically through a JSON GET call. Upon inspecting the tree in the React Inspector, I noticed that each element has multiple props associated with it. H ...

Having trouble with updating a document in Mongoose - encountering an issue where the save function is not recognized

I'm currently working on a project where I am practicing CRUD operations with Node, MongoDb, and Mongoose for database management. However, I have encountered a roadblock when attempting to update my data. Let me share the code snippet in question: / ...

Hover over to reveal sliding menu

Hey everyone! Take a look at this code snippet: <body> <div id="slidemenubar"> <a href="">1</a> </div><br> <div id="slidemenubar2"> <a href="">2</a> </div> </body> When hovering over the ...

While React remounts every component during development, it is puzzling that the screen only shows the UI snapshot of a single component

Participating in this React challenge presents a curious scenario. Even though the UI of DOM is only displayed on the screen once, the Counter component is being re-mounted during development. This behavior causes the setInterval function to run twice af ...

Retrieve all elements from an array that have the highest frequency of occurrence

Consider an array like [1,4,3,1,6,5,1,4,4]. The element with the highest frequency in this array is 3. The goal is to select all elements from the array that have a frequency of 3, so in this case we would select [1,4]. To achieve this, one possible meth ...

The Jquery Index() method often results in a return value of -

I have developed a function in JavaScript that creates HTML content. Within the test(test1) function, I am checking if test1.Test__c is null and changing the color accordingly. The line ".table-striped > tbody > tr.testcss" is responsible for changin ...

Leverage React.JS to iterate through arrays and objects, rendering data seamlessly - even within nested objects

Development of Category's Filter component Implementing the rendering of data from a nested array using React Js Although I attempted to render it as seen below, it is not displaying anything The main focus is on the rendering part only var selecte ...

Transforming an array's value into an input field is essential

Check out my awesome array! $xyz = array ( '2' => '2', '4' => '4', '6' => '6', '9' => '9', '14' => '14', '18' ...

JavaScript Error: The code is expecting an object when creating a new instance,

Why isn't this code working as expected? var navbar = document.getElementById("navbar").getElementsByTagName("li"); var hover; var moname; var slider; var newPos=new Object(); var body=document.getElementsByTagName('body& ...

What is the best approach to slowly transition a value to a different one over a set period of time?

if(!isWalking) { isWalking = true; var animation = setInterval(function () {$player.css({'left': "+="+boxSize/25})}, 10); setTimeout(function(){clearInterval(animation)},250); setTimeout(function(){isWalking = false},250); ...

Tips for utilizing the value of object1.property as a property for object2

Within the template of my angular component, I am attempting to accomplish the following: <div> {{object1.some_property.(get value from object2.property and use it here, as it is a property of object1)}} </div> Is there a way to achieve this ...

Non-functioning function within object

this is a unique object var Manager = (function () { var self = this; self.fetch = function (request, response) { response.send({ message: 'Data fetched successfully' }); } return self; })() module.ex ...