Maintaining equal heights for divs in jQuery upon resizing

I have been attempting to dynamically set the height of all my divs to match the highest div upon page load and window resize. Below is the code snippet I am using:

// function to equalize column heights
function eqColumn(){
    if ($(window).width() > 767){
        var item = $(".item");
        var biggest = 0;
        $(item).each(function(){
            if ($(this).height() > biggest){
                biggest = $(this).height();
                console.log(".item height = " + biggest);
            }
        });
        item.css("height", biggest);
    }
}

// event binding
$(window).on("load resize", eqColumn);

The code works as expected on initial load, but encounters issues when resizing the window. Upon logging the value in the console during a resize event, the value remains unchanged. I am puzzled by this behavior. You can view an example site here:

What could be causing this issue?

Answer №1

The reason for this behavior is that all items have the same size after loading or resizing because they are all resized when the eqColumn function is first executed:

item.css("height", biggest);

To fix this, try resetting the size of .item before iterating through each item:

function eqColumn(){
if ($(window).width() > 767){
    var item = $(".item");
    //Reset height to auto
    item.css("height", "auto");
    var biggest = 0;
    $(item).each(function(){
        if ($(this).height() > biggest){
            biggest = $(this).height();
            console.log(".item height = " + biggest);
        }
    });
    item.css("height", biggest);
}
}

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

Increasing the level of CSS list counters

HTML <ol> <li>item1 <ol> <li>item2</li> </ol> </li> <li>item1 <ol> <li>item2</li> <li>item3</li> </ol> </li> </ol> SC ...

Joining arguments beyond argv[2] in a Node.js application

Recently, I received a suggestion from Mykola Borysyuk to use node-optimist. However, the author mentioned that it's deprecated and recommended using Minimist. Even after going through the information, I have a basic understanding. Can someone provid ...

Issues with javascript functionality in Internet Explorer version 9 and higher

Currently, there is a flash music player (audioplay) embedded on a website. Despite personal preferences against having music on websites, it was requested by the client. The functionality in question involves using JavaScript to trigger "stop" and "play," ...

Adding a script to the head of a Next.js page by using the Script component

I need assistance with inserting tracking code from Zoho application into the Head section of each page in my Next.js application. I am currently using a _document.tsx file and following the instructions provided by Next.js regarding the use of the Next.js ...

I am experiencing an issue with the display inline feature not functioning properly in Firefox

I am working with Html code: <div class="login"> <form class="form-horizontal signin"> <div class="form-group"> <label for="inputEmail3" class="col-sm-2 control-label">Ema ...

I would like to connect the button to a different webpage

Is it possible to connect this code with page number 2? <div class="login"> <h1>Login</h1> <form method="post"> <input type="text" name="u" placeholder="Username" required="required" /> <input type="password" name="p" ...

When the page first loads, the slider is responsive, but it does not adjust

I installed the Moving Boxes plugin and customized it to create a full-width image slider. While it works well when the page loads, I'm facing challenges with making it responsive on resize. It appears that the JS script sets fixed widths upon load, w ...

Error: Unable to parse Signup parameters: JSON decoding failed to convert object into a string type for the field SignupParams.email in Go struct

I've been working on setting up Supabase with email and password authentication for user sign-ups, but I keep encountering this error: AuthApiError: Could not read Signup params: json: cannot unmarshal object into Go struct field SignupParams.email o ...

Sending JSON data results

I received this JSON response: {"data":[{"series":{"id":"15404","series_code":"TOS","publisher_id":"280","series_short_name":"Tales of Suspense","start_year":"1959","end_year":"1968","published":"1959-1968","type_id":"1","no_issues":"99","published_ ...

Setting up configurations in the prettyPhoto open method

Is there a way to create a div using jQuery's prettyPhoto plugin as follows: $.prettyPhoto.open('images/fullscreen/image.jpg','Title','Description'); I also want to include the modal: true option and hide the close butt ...

By changing the name of my file to .js.erb, I am able to seamlessly incorporate erb syntax within my javascript code, but this

Currently, I am using chessboard-0.3.0.js in a rails application. Due to the rails asset pipeline setup, I've had to make adjustments to the js code specifically related to rendering images, such as Pieces. In line 445 of chessboard.js, it states: c ...

Struggling with CSS to display images

Struggling to align my images within the designated red lines in the CSS sections I created. Can't seem to get it right. Here's my HTML: <!-- Your code goes here --> <div class="container"> <section class="left"> <h1& ...

Using $.each instead of a traditional for loop in jQuery can make your code

I need to add five list items (li) to the body using the jQuery each() function, but it doesn't seem to be working as expected. The goal is to append a total of five li elements to the body. Please note: This code snippet is just for demonstration pur ...

Click the close button within a jQuery dialog to close the text

Why is my jquery dialog displaying both the Close text and the icon? Is there a way to hide the Close text? This is the code I am currently using: <script src="https://code.jquery.com/jquery-1.12.1.js"></script> <script src="https://code. ...

Revealing a concealed element by sliding it upwards on a single page, then repeating the

Can someone assist me in achieving a specific effect on my website? I have a top banner section with a button at the bottom. When the button is clicked, a form should slide up into view. The form will initially be hidden. I also need the same effect at the ...

vee-validate remains consistent in its language settings

Having trouble changing the error message in vee-validate, any suggestions on how to fix this? import VeeValidate from "vee-validate"; import hebrew from "vee-validate/dist/locale/he"; Vue.use(VeeValidate, { locale: "he", dictionary: { he: { me ...

How can I incorporate a CDN link into the newly updated Next.js App Router?

Exploring next.js has been quite an adventure for me, and I'm impressed with its capabilities. However, being a beginner, I have encountered some challenges. One issue I am currently facing is the difficulty in using Google icons without a CDN link in ...

Intermittent issue with anchor tag function within a div container

Having trouble with my anchor tag inside a div that should act as a popup. The anchor is clickable but doesn't always navigate to the URL in href. I can't figure out what's causing this intermittent issue. Any assistance would be greatly app ...

The Google Pie chart is displaying outside of the designated div area when placed inside a dropdown menu

Encountering an issue with my pie chart rendering outside of its parent div when placed within a dropdown menu. The chart successfully loads after the page is loaded, but only displays correctly if I hover over the dropdown and allow it to load. If I do ...

What are some ways to establish a connection with the store in components that are not using React

It's been a struggle trying to connect to the store using non-react components. Whenever I attempt to use getState or dispatch in a non-react class like this: createStoreWithApi(api).dispatch(isLoading(true)), it ends up creating a new instance of the ...