Creating HTML Divs with Equal Heights

I have written this JavaScript code, but I feel like there might be some redundancy. Does anyone have suggestions on how to improve and optimize this code?

var boxHeights = [];
$('.box').each(function() {
    boxHeights.push( $(this).outerHeight() );
});
var maxHeight = Math.max.apply(Math, boxHeights);

$('.box').each(function() {

  $(this).css('height', maxHeight+'px');

});

Answer №1

Check out this concise alternative. Avoid the extra loop by calculating and setting the maximum height for all .box elements in one go.

var heights = [];
$('.box').each(function() {
    heights.push( $(this).outerHeight() );
}).css('height', Math.max.apply(Math, heights)+'px');

Answer №2

Here is a method for optimizing code that I have come up with:

$(function(){

var maxHeight = 0;
$('.box').each(function() {
    var h = $(this).outerHeight();
    if(h>maxHeight) maxHeight=h;
});

$('.box').each(function() {
  $(this).css('height', maxHeight+'px');
});

});

Check out the Fiddle

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

Chrome not displaying Bootstrap dropdowns correctly

Lately, I've been exploring Bootstrap and experimenting with its dropdown menus. Typically, I use Chrome for my work but encountered a strange issue with how Chrome is handling my dropdown menus. This forced me to temporarily switch to Edge. Here&apos ...

Create a homepage landing page that only displays occasionally

Uncertain about the best way to phrase this question, a Wordpress user requested my help in creating a homepage that can branch off into two sections without loading every time a user visits the "home" page. Imagine visiting xyz.com and being greeted with ...

The YouTube video continues to play even after the container is closed

I recently worked on implementing a lightbox feature using jQuery, where a window opens upon clicking an element. Everything was working fine with images, but when I tried to open a YouTube video and play it, the video kept playing in the background even a ...

Is it possible to avoid widows in CSS without the need for extra HTML elements?

I have a snippet of text that is controlled within a Content Management System. The CMS restricts the use of HTML or special characters such as &nbsp; or <span>. Despite my attempts to adjust margins and padding, I cannot achieve the desired out ...

Guide on how to pass the chosen dropdown value from a custom component back to the main component

I've developed a customized MUI select component in React and have integrated it multiple times within another component as shown: customDropdown.js import * as React from 'react'; import FormControl from '@mui/material/FormControl&ap ...

Challenges arise when utilizing the foundation 6 grid for image overlays within a React application

Having some trouble using the foundation 6 xy grid for the first time and aligning three images with text overlays at the bottom of each image. I am struggling to make the background of the text fill the full width of the image while also ensuring it is re ...

The Unshift function in Reactjs uses Jquery to dynamically generate new buttons whenever it is clicked

When utilizing ReactJS in combination with jQuery, the Unshift function is causing an issue where additional buttons are being created upon clicking. The code snippet was sourced from the renowned platform Stack Overflow, and it involves an array of three ...

Transforming table data into a JSON format

My goal is to generate a specific JSON format from a table. The table consists of rows and 4 columns. You can view my table here. I aim to create a JSONArray based on the table. The first value in the left column serves as the key in the JSON, while the la ...

Tips for setting up Highcharts tooltip.headerFormat using the function getDate() plus 5

I'm facing a little challenge trying to understand how the JavaScript function getDate interacts with Highcharts datetime on xAxis. My goal is to show two dates in the tooltip header, forming a date range like this: 1960/1/1 - 1965/1/1. The first da ...

The process of showcasing angularjs ng-repeat with a JSON object

I'm attempting to search for product results using AngularJS. I have retrieved a JSON object with my results, an example of which is shown below: [{"store_id":"17","user_id":"29","company_name":"Liquor R Us","company_type":"Alcohol", "phone":"(303) 5 ...

Exploring the power of jQuery.grep for locating specific keywords and phrases

Struggling to figure out how to extract keywords and phrases from a textarea by using jQuery.grep in a way that does not require an exact match but rather checks if it contains specific keyword(s) and/or phrase(s) in an array. Currently, the code provided ...

Developing JSON with the use of jQuery

My latest project involves creating an application that converts HTML tables to JSON format. The code is functioning properly, but I've encountered an issue when the td or th elements contain inner components like span or other child elements. While I ...

Retrieving checkbox values from a MySQL database in HTML/PHP

My database contains data stored in varchar format, such as S,M,L,XL. On my PHP page, I have checkboxes corresponding to these options. Is it feasible to fetch this data from the database and display the pre-checked checkboxes in HTML using PHP? (For exa ...

Use JavaScript to switch the h1 title when selecting an option from the dropdown menu

As a beginner in coding, I am struggling to find a solution using an if statement for this particular problem. Although I can achieve the desired result with HTML code and options, this time I need to use arrays and an if function. My goal is to create a ...

Update the content of a div element without having to fetch an external file

I have a question regarding using JQuery with PHP and MySQL. Here's the situation: I have a div that displays a user's points, but the points change due to various actions on the website. I've come across many scripts that use external file ...

What is the best way to insert an iframe using getElementById?

I am looking to make some changes in the JavaScript code below by removing the image logo.png lines and replacing them with iframe code. currentRoomId = document.getElementById('roomID').value; if ( document.getElementById('room_' + c ...

Angular 11 - Binding Data to an Array Element

Currently, I am attempting to apply data-binding to specific properties of an element categorized as Ripe: export interface Ripe{ version: string; data_call_status: string; cached: boolean; data: { is_less_specific: boolean, ...

A Guide on Integrating a Javascript Reference into HTML while Displaying a PHP Object

Our website relies heavily on PHP modules to generate objects that are essential for constructing our web pages. Some of the key modules we have include: Anchor (ahref) Button CheckBox ComboBox DateTime Email Label Note Password Phone RadioButton RichTe ...

"Struggling to horizontally center a div with a fixed width in CSS - what am I

Whenever I try to use it on my browser, the content shifts to the left and I can't seem to center it using text-align or flexbox. The issue gets resolved when I remove the width property, but I really need it to have that paragraph-style look I'm ...

A guide on accessing objects from an array in Vue.js

Wondering how to choose an object from an array in Vue.js: When the page loads, the selectTitle() function is triggered. I simply want to select a specific object (for example, i=2) from my 'titleList' array. However, at the moment, I am only re ...