Adjust all children's height to match that of the tallest child

My nearly completed jQuery slideshow is working well except for one issue - the height of the slideshow remains fixed at the height of the first displayed slide, causing problems. View the issue here: . I have tried different jQuery solutions from StackOverflow without success.

To resolve this, I'm trying to loop through each .slideshow element on the page, then iterate over the slides in each slideshow to find the largest height and adjust all the slides accordingly. Here's the code I attempted:

var maxheight = 0;
$('.slideshow').each(function() {
    $(this).children().each(function(){
        maxheight = ($(this).height() > maxheight ? $(this).height() : maxheight);
        console.log(maxheight);
    })
    $(this).children().height(maxheight);
    console.log('Final:' + maxheight);
    maxheight = 0;
})

However, this code messed up the slideshow completely as shown in this JSFiddle: http://jsfiddle.net/B4rqz/

If anyone can offer guidance on solving this issue, I would greatly appreciate it. Here's the HTML and CSS snippets:

HTML:

(HTML snippet here)

CSS:

(CSS snippet here)

jQuery:

(jQuery snippet here)

View the complete set-up on JSFiddle for reference: http://jsfiddle.net/B4rqz/

Answer №1

It is important to remember that when setting the height of children elements, you also need to set the height of the parent element.

let maxHeight = 0;
$('.slides').each(function() {
    $(this).children().each(function(){
        maxHeight = ($(this).height() > maxHeight ? $(this).height() : maxHeight);
        console.log(maxHeight);
    })
    $(this).height(maxHeight).children().height(maxHeight);
    console.log('Final Height:' + maxHeight);
    maxHeight = 0;
})

Answer №2

Consider utilizing deferred objects in your code

Make sure $(this).children().height(maxheight) doesn't run before the completion of each loop as JavaScript operates asynchronously

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

Choose the div element that is immediately before the current div

Attempting to create a harmonica effect by adjusting the height of divs upon hovering over another. The HTML is structured as follows: <html><body> <div class="section1"></div> <div class="section2"></div> <di ...

The Challenge of CSS Transition and Checkbox Label Discrepancies

I'm looking to adjust the position of my label when a checkbox is checked. Everything works smoothly if I don't include a transition for the top offset property in the CSS of my label. However, when this transition is added (as seen in the commen ...

How can I create a dropdown menu that is dependent on another dropdown menu using Ajax in my Laravel application?

I have two dropdown fields that are dependent on each other - Class & Section. I am trying to Select * from sections where class_id=selected Class Id. Although I attempted to achieve this using java script, it doesn't seem to work for me. Here are ...

Switching the character from "." to "|" between portfolio categories on a WordPress website: Step-by-step guide

Visit Portfolio Page I have an unusual request - I need to replace the tiny dot between "Graphic Design" and "Website Design" with a vertical bar "|". I thought I could do this in portfolios.php, but I can't locate the dot in the file. In the inspect ...

Exploring the Iteration of Ajax in ASP.NET MVC

I am trying to extract data from an object retrieved in my view through the use of $.getJSON. This object consists of several Lists that require iteration. Can someone please guide me on how this can be achieved? Here is the snippet I have so far: $.getJS ...

An issue occurred: TypeError - Unable to access the 'subscribe' property of an undefined object during a POST HTTP request in Angular [8]

I'm currently attempting to send data to a REST API using Postman. I am encountering an issue where I receive the error "Cannot read property 'subscribe' of undefined" when making a POST HTTP call, as shown in the console log: https://i.sta ...

Tips for retrieving the dynamically generated ID within an li tag

I've been diving into the world of JavaScript and jQuery, encountering a few hurdles as I attempt to merge various solutions that I come across. This code represents a mishmash of tutorials I've recently completed. Admittedly, I am quite new to ...

What is the best way to start tiny-slider automatically once the video has ended?

I am currently using the tns-slider plugin and have a setup with 3 slides (2 photos and 1 video). <div class='tiny-slider'> <div class='slide slide1'> <div class='video-slide'> <video id=&qu ...

In Bootstrap 4, the positioning of :after is different compared to Bootstrap 3

Our bootstrap nav-tabs have a unique default look with an 'indicator' icon below the selected tab. This is achieved by styling the active anchor element as follows: https://i.sstatic.net/UQos5.png However, when migrating to bootstrap 4, we noti ...

``I am facing an issue where the jQuery html() function is not properly

I am struggling with a div <div id="box-loading-txt">txt</div> Despite my efforts, this jQuery code doesn't seem to be working properly. The content in the div always ends up showing as "text B text B text B" even on the initial html() ...

JointJS: I need to extract the source value from the JSON text with the ID field

I need to extract the text value from "source":{"id": using JSON data in this specific JavaScript object : { "cells": [ { "type": "devs.Model", "size": { "width": 40, "height": 40 }, "inPorts": [""], "outPorts": ["" ...

The Ineffectiveness of the Form Class

I have been trying to personalize the appearance of my bootstrap form by applying my own custom CSS. However, despite creating a class for my form and making changes to its CSS properties, the style of the form remains unchanged. Take a look at my form: ...

Using jQuery to fetch and display content from an external webpage

Is there a more effective method for loading an external web page on the same server? I've experimented with .load() and .get(), but they only load the page after the PHP script is finished. I've also used an iFrame, which displays the informatio ...

The navigation bar's background color remains static and doesn't update as

Struggling with HTML/CSS and unable to get the NAV bar to display the specified background color. Tried putting the nav in a class, commented out parts of the code, but still couldn't make it work. Despite looking for solutions, I can't figure ou ...

Attempting to invoke a jQuery function from the code behind in ASP.NET appears to be ineffective

After adding a record to the database, I am trying to call a jQuery function... ScriptManager.RegisterClientScriptBlock(LbOk, typeof(LinkButton), "json", "topBar('Successfully Inserted');", true); To execute the jQu ...

Trouble with executing action after submitting form using jQuery and AJAX

I'm currently working on implementing form validation and submission using jQuery and AJAX. For the most part, everything is functioning as expected. However, there is a small snippet of code causing an issue: alert ('Validation success'); ...

Prevent users from inputting multiple minus signs by setting the input type to number and disabling the

Is there a way to prevent the input type number from accepting "--" as input? I am looking for a solution to disable this behavior. Additionally, the input field seems to allow for various incorrect formats like: -12-12 -- -121- 23-323- ...

Updating a jQuery function to accommodate dynamic elements by employing either the .on() method or .delegate() method

I am looking to convert my jQuery function to .live so that it can be bound to elements added dynamically on the webpage. Here is the current jQuery code snippet: $(document).ready(function(){ $('form.ratedStars').each(functio ...

Google/StackExchange Menu Tab

I am interested in creating a navigation bar similar to Google's or StackExchange's new one, where it is always positioned at the top. It does not need to be fixed there, but I would like it to have links only. How can I achieve this layout with ...

Using the mouse scroll to activate the $anchorScroll effect in Angular instead of a click

I have been searching for a solution to my problem without any luck so far. Hopefully, someone here can provide guidance and help me find a solution. My goal is to replicate the behavior shown in the second example on the following page: https://docs.angu ...