When using JQuery's .each method, only the first two elements from an array of automatically generated elements are retrieved

I have a loop that creates multiple divs with the class panel.

@for(comment <- event.getCommentsSorted()) {

However, when I try to manipulate each of these divs using jQuery's .each, only the first two divs are selected.

$(window).on('load', function() {
        $(".panel").each(function (index) {
            alert(index);
            $(this).height($(this)[index].scrollHeight - 12);
        });
    });

The remaining three divs seem to be missing.

https://i.sstatic.net/hpLmq.png

I initially thought it could be due to the script executing before all divs are created, but since I'm using load, it should run after the page has fully loaded. I've also tried utilizing .ready and varying the number of generated divs, but I still only target the first two elements.

Why am I only able to select the first two elements, and is there a way to target all elements?

Answer №1

To improve performance, consider wrapping the .each method within a setTimeout function as shown below:

$(window).on('load', function() {
  setTimeout(function() {
    $(".panel").each(function (index) {
      alert(index);
      $(this).height($(this)[index].scrollHeight - 12);
    });
  }, 3000); // Execute after a delay of 3 seconds
});

Answer №2

After searching for a solution, I finally found it!

$(this).height($(this)[index].scrollHeight

wasn't getting the job done like I expected.

For some reason unknown to me, switching to this code made everything work perfectly:

$(".panel .inputSizeLimitation").each(function () {
       $(this).height($(this).prop("scrollHeight") - 12);
    });

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

Is there a method to obtain the compiled CSS for an Angular 2+ component?

Is it possible to retrieve compiled CSS (as a string) from an Angular2+ component? @Component({ selector: 'test-graph', templateUrl: './test-graph.component.html', styleUrls: ['./test-graph.component.scss'] }) export cla ...

Issues with AngularJS Opening the Datepicker upon Click

Currently, I am developing an application using AngularJS, JQuery, and Bootstrap. In this project, I have incorporated a customized date range picker from www.daterangepicker.com. Issue: The problem arises when I attempt to open the date range picker by c ...

Is it possible to dynamically change the color of a box shadow using an event handler?

I'm currently in the process of developing an application that consists of six distinct topics organized within a flexbox structure, complete with a box-shadow effect. My objective is to dynamically alter the color of the box-shadow through an event ...

Trigger an event in JQuery when a selector changes

Here is my HTML code with a file input: <input type="file" id="file"> I initially tried to add an onChange event listener like this: $("#file").change(function(){}); Unfortunately, it did not work as expected. I had to modify the code to make it ...

Using jQuery within MediaWiki pages beyond just Monobook.js or extensions is a powerful tool

Currently, I am experimenting with some jQuery functionalities in a MediaWiki content page to enhance various features. I have referred to the following resources: http://www.mediawiki.org/wiki/Manual:$wgRawHtml (particularly for incorporating Paypal but ...

Ways to subtly adjust the edges of text in a design

https://i.stack.imgur.com/sr4PF.pngIs there a way to adjust the border of text that already has a border applied? I have successfully created the text with the border using the following CSS: h1 { font-size: 35pt; text-align: center; font-family: ...

Using AJAX for fetching and saving an object into a variable

I am dealing with two specific files in my project. The first one is called index.php, where the user initiates an AJAX request. The second one is called process.php, which is responsible for sending data back to the index.php file. function AjaxResponse( ...

When an option is selected, the CSS class associated with that option is removed

<select class="FunctieSelect"> <option class="yellow" value="-1">- kies -</option> <option class="yellow" value="1">KSZ functie</option> <option class="yellow" value="2">Bakker</option> <option class="yellow" va ...

Identifying the block on the screen using Jquery as you scroll

Hello everyone, I have a few questions and would appreciate some help. Let's say I have a certain number of div blocks: <div class="main"> <div class="box block1"></div> <div class="box block2"></div> ...

Leveraging the Scroll feature in Bootstrap for smooth scrolling

Seeking assistance with implementing scroll in Bootstrap 5 on my child component (ProductList.vue). Can anyone guide me on how to integrate the code? I have been searching for a solution without success. Below is the Bootstrap 5 code on the child component ...

The issue of sum calculation not functioning properly when manually inputting values in jQuery

In my current project, I am in the process of calculating the week count, Non week count, and Row Total based on the provided dates. You can view a demo of the functionality on JSFiddle here. This is a snippet of my HTML code: Your HTML code goes here.. ...

Issue in Opera with the $(window).load(function(){}); script

In order to display the body's main div height correctly after all content (images) have loaded, I utilized a combination of jQuery and CSS. //The CSS used is as follows body {display: none;} /* Function to calculate div block height */ function re ...

The Bootstrap 5 class "col-md-4" seems to be malfunctioning

My goal is to create a grid layout with 3 columns and 2 rows using Bootstrap cards, but I'm encountering an issue where all the cards are centered and stacked on top of each other. Can someone please assist me in resolving this problem? Service.js co ...

Issues with refreshing Datatables functionality

I’ve hit a roadblock while troubleshooting this issue, and it’s gotten quite frustrating. That's why I've turned to Stackoverflow for help once again. As a beginner, I ask for your patience and thank you in advance for any assistance. In my ...

Error: The variable $ is not recognized during the import process

One file, bundle.js, contains the following: window.$ = window.jQuery = require('jquery'); import './app.js'; While app.js contains this code: $('#button').on('click', function(e) { ... }); As a result, I enc ...

Mastering the Art of Scrolling

Can someone please tell me the name of this specific scrolling technique? I am interested in using something similar for my project. Check out this example site ...

Header reference differs from the document referrer

this is the request header: GET / HTTP/1.1 Host: localhost:3002 Connection: keep-alive sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="96", "Google Chrome";v="96" sec-ch-ua-mobile: ?0 User-Agent: ...

How can CSS files be shared among multiple projects within Visual Studio 2012?

I am working on a Visual Studio Project Solution that includes 3 separate projects. To streamline the process and avoid duplication, I aim to have a shared CSS file for all 3 projects since they all follow the same CSS rules. Despite trying the Add As Lin ...

What sets justify-content apart from align-items?

I'm really struggling to grasp the distinction between these two properties. After conducting some research, it appears that justify-content can handle... space-between and space-around, while align-items is capable of... stretch, baseline, initial, a ...

Validating SASS based on class name

Hi there, I am fairly new to SASS and do not consider myself a programming expert. I have ten aside elements that each need different background colors depending on their class name. Even after going through the SASS documentation, I am still unable to f ...