Modifying Div Size with Jquery

I am working on populating the div container with square boxes, but I'm having trouble adjusting the size of my #gridSquare div. Despite trying to change its height and width using jQuery, it doesn't seem to work as expected.

$('#gridSquare').css({"height": "38.4", "width": "38.4"});

You can find the fiddle containing the rest of my code linked below for reference.

Thank you for taking the time to review this!

Fiddle

My ultimate goal is to utilize the squareSide variable to dynamically set the height and width of the squares.

Answer №1

To ensure proper functionality, utilize a class instead of an id (ids should be unique). Modify the CSS of the square after appending it to the squares:

var squareSide = 960 / 25;

console.log(squareSide);

for (var rows = 0; rows < 25; rows++) {
  $('<div class="gridSquare"></div>').appendTo('.container')
  for (var cols = 0; cols < 25; cols++) {
    $('<div class="gridSquare"></div>').appendTo('.container')
  }
}

$('.container').on('mouseenter', '.gridSquare', function() {
  $(this).css('background-color', 'white');
});

$('.gridSquare').css({
  "height": "38.4",
  "width": "38.4"
});
.container{
background-color: grey;
margin: 0 auto;
text-align: center;
font-size: 0;
margin-bottom: 30px;
width: 960px;
height: 960px;
}

.gridSquare{
background-color: black;
display: inline-block;
vertical-align: top;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container"></div>

Consider updating your for loop like so:

// 625 = 25 * 25
for (var i = 0; i < 625; i++) {
    $('<div class="gridSquare"></div>').appendTo('.container')
}

Answer №2

Most likely

$('#gridSquare').css({"height": "38.4px", "width": "38.4px"});

Answer №3

Instead of using an id, opt for a class.

Include the following code snippet:

$(document).ready(function(){
    var squareSide = 960/25;

    console.log(squareSide);

    for(var rows = 0; rows < 25; rows++){
    $('<div class="gridSquare"></div>').appendTo('.container')
        for(var cols = 0; cols < 25; cols++){
            $('<div class="gridSquare"></div>').appendTo('.container');
            $('.gridSquare').css({"height": "38.4", "width": "38.4"});
        }
    }

    $('.container').on('mouseenter', '.gridSquare', function(){
        $(this).css('background-color', 'white');
    });

});

Check out the Fiddle here: http://jsfiddle.net/5ojjbwms/6/

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

Issue encountered while trying to display images next to each other using only HTML and CSS

I am facing an issue with the vertical space between the top and bottom rows of my portfolio. There seems to be some unwanted space in the rows that I cannot account for. It doesn't seem to be a margin or padding within the box, so I suspect the probl ...

Changing the value of a previous TD element using Javascript

My page features a button that, when clicked, triggers a function to change the value in the previous TD element from "No" to "Yes". Here is the HTML snippet: <tr class="odd"> <td class="">13.00</td> <td class="">DDR</td> ...

What is the best way to configure my AngularJS routing for managing URL rewriting and page reloads effectively?

As I develop my website using AngularJS, one of my main goals is to create a smooth navigation experience without the annoying "browser flash" effect that occurs when switching between pages. This means that clicking on a link in index.html to go to foo.ht ...

Footer void of color

It's puzzling to me where the extra space in my footer is coming from. It seems to be around 20-30px as you can see on the following url: Despite the code in my footer being minimal, with no apparent cause for this additional space below my social ic ...

tips for dynamically highlighting search bar after choosing a different dropdown option - vuejs

I need to filter products in my application based on name and category. To achieve this, I have included a search text box and a dropdown select box. The dropdown select box offers two options: 1. Search products by name 2. Search products by category name ...

Explore the possibilities of using a unique custom theme with next.js, less, and ant design

Trying to customize the default theme in antdesign has been a challenge for me. I've switched from sass to less, but there seems to be something that just won't work. I've exhaustively searched online for solutions - from official nextjs ex ...

The navigation bar is obscuring the header on my website

I am facing an issue with creating a navigation bar that covers up the header text. I tried adjusting the margins to fix this problem but ended up moving the header along with the navigation bar. How can I ensure that the navigation bar remains fixed witho ...

Encountering the error "fn.dataTable.moment is not a function" when trying to use momentjs with DataTables in Angular

Feeling a bit frustrated as I struggle with what seems like a simple issue to fix. Using a DataTable that is working fine, but now I want to make a column of dates sortable in the German date format dd.MM.yyyy. Found the documentation on the official dat ...

Stop the execution of the setTimeout function when the webpage loads in JavaScript

In my postgres database, I have a table named students for storing student information. The structure of the table is as follows: id first_name last_name time_remind 1 pers1 pers1_name 2022-07-30 21:30 2 pers2 pers2_name 2022-07-30 20:38 Current ...

Inadequate termination of the while loop

I'm trying to generate a line of text in JavaScript running in Node, ensuring that it does not exceed a certain number of syllables. To achieve this, I have created a function utilizing the "syllable" library as follows: function generateLine(maxSyll ...

Utilizing visual representations for "symbol" within eCharts4r

I have been exploring the use of the "image" option for the symbol parameter in a tree chart with eCharts4r. Despite trying multiple methods, I am struggling to assign a unique image to each node in the tree instead of using a universal one. However, my a ...

What is the best way to update the background color for specific days in fullcalendar?

I have been working on my fullcalendar code and I am trying to figure out how to change the background color only for Feb 14. dayRender: function (date, cell) { var Xmas = new Date('2017-02-14'); var weekday = Xmas ...

Checkbox remains selected even after the list has been updated

I am currently dealing with an array of objects where each object has a property called "checked." When I click on a checkbox, it becomes checked. However, when I switch to another list, the checkmark remains even though it should not. Here's an examp ...

I must arrange each item in a column vertically, regardless of whether they could align horizontally

As the title suggests, I am looking for a way to stack one element under another, similar to a chat application. Currently, when the message is long, it displays correctly, but if the text fits next to each other, it aligns in that manner. I require a solu ...

Tips for identifying the most effective element locator in the DOM with Playwright

Currently, I am in the process of incorporating Playwright tests for a website that supports multiple locales. The majority of the page content is dynamically generated from CMS content (Contentful). I am hesitant about using hardcoded text locators like ...

What is the best way to align content in the center when its parent element has a position property

JSBIN Is it possible to center the number row without using JavaScript to calculate its position, considering its parent has a fixed attribute? I've tried using CSS properties like margin and text-align with no success. Any suggestions on achieving t ...

After refreshing the page, the jQuery AJAX item becomes unclickable

Currently utilizing Django Within the body block <div id="tag_like"> {% if liked %} <img id="unlike" title="unlike" src="{{ STATIC_URL }}pic/bad.png" /> {% else %} <img id="like" title="like" src="{{ STATIC_URL }}pic/go ...

Updating divs automatically using Ajax without refreshing the page is not functioning as intended

I'm having trouble understanding why this isn't functioning properly. My goal is to have the data from load.php displayed in the div sample. However, if the code is updated (for example, when pulling information from a database), I want only the ...

Linking two div elements together with a circular connector at the termination point of the line

I am currently working on designing a set of cards that will showcase a timeline. I envision these cards to be connected by lines with circles at each end, for a visually appealing effect. At the moment, I have created the cards themselves but I am struggl ...

Showing a cell border when a radio button is chosen

I am working with a table that contains input/radio buttons, and I am trying to figure out how to apply a border to a specific cell when the user selects the radio button within it. This will give the appearance of the cell being selected. I am not sure ho ...