Utilizing jQuery for Dynamically Loading Child Elements

As users select choices from a side menu, a set of items is dynamically added. The more choices they make, the more items are displayed.

Some items have a 'stats' div while others do not. I want to check if an item has a 'stats' div, and if it does not, increase the width of the 'more' div so that it fills up the empty space left by the missing 'stats' block.

Below is the HTML code for the generated items:

<div class="item" idse="2116082">
                    <div class="ico"></div>
                    <div class="ID">2237</div>
                    <div class="Time"><span>00:00</span><span>06 Dec</span></div>
                    <div class="Event"><span>Test</span><span>Test</span></div>
                    <div class="stats">Stats</div>                        
                    <div class="more">+ 3</div>                        
                    <div class="odds">                                                                                                                      
                                <div class="odd r1 c1 g1" id="td_448023666">
                                <div title="Home">Home</div>
                                <div>1/2</div>
                                <div class="hnd"></div>
                                </div>                                                                                                                                                                                                                       
                                <div class="odd r1 c2 g1" id="td_448023667">
                                <div title="Draw">Draw</div>
                                <div>7/2</div>
                                <div class="hnd"></div>
                                </div>                                                                                                                                                                                                                       
                                <div class="odd r1 c3 g1" id="td_448023668">
                                <div title="Away">Away</div>
                                <div>9/2</div>
                                <div class="hnd"></div>
                                </div>
                                <div class="sepOdds"></div>                     
                                <div class="endOdds"></div>                                                  
                    </div>
                </div>

Here is my jQuery function:

$('body').on('load', '.item', function () {
        if (!$(this).children('.stats')) {
            $(this).children('.more').css('width', '132px');
        }
    });

Answer №1

Is this the kind of solution you're looking for?

$('document').ready(function () {
    $(".item").each(function(){
        if (!$(this).children('.stats').length) {
            var more = $(this).children('.more');
            var more_width = more.width();
            more.css('width', more_width + '132px');
        }
    });
});

View entire code on JSFiddle

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

Easily transfer multiple files simultaneously to the server using Summernote File feature

How can I modify the code below to allow my upload.php file to receive and process multiple files? I have been attempting to upload several files simultaneously to a server using Summernote. However, despite selecting multiple files for upload, only one fi ...

Ways to ensure <li> elements remain anchored to the top and bottom of <ul> element while scrolling

I currently have a ul with a fixed height that contains a dynamic number of li elements. When there are too many elements, a scrollbar appears. By clicking on a li element, you can select it. What I am aiming for is to have the active li element stay fixe ...

Is there a way to create a new perspective for Ion-Popover?

Looking for a solution: <ion-grid *ngIf="headerService.showSearch()"> <ion-row id="searchbar" class="main-searchbar ion-align-items-center"> <ion-col size="11"> ...

JavaScript: Conditional statement used to determine the target of a touched element

I'm currently working on enhancing the mobile responsiveness of a React project. As part of this process, I am attempting to ensure that certain JavaScript-driven style changes are only applied to elements that are not the target or a child of: <d ...

Using PHP to redirect upon clicking a link

My issue involves directing users to another page or form and saving the button they click as a value when selecting a CRN (course number). <table class="table table-bordered table-hover table-striped"> <thead> ...

Growing background picture

I currently have an iPad serving as a background image. However, I desire for it to be expandable, with the possibility of it being integrated into the header, footer, and the middle section expanding as needed to fill the entire page. I am open to any a ...

What could be causing the NoScript tag to malfunction across different web browsers?

I've incorporated the NoScript tag into my JSP pages in the head section. To avoid conflicts with tiles, I made sure not to include multiple NoScript tags. However, I am experiencing issues in Internet Explorer where it doesn't seem to be working ...

ASP.NET Web Forms returning results in JSON

Using asp.net and web forms, I have an asmx web service in my project. [WebMethod] public string GetSomething() { // avoiding circular references (parent-child) List<RetUsers> res = repo.GetAllUser().Select(c => new RetUsers { ...

Chrome glitch: how to make radio buttons bigger

Hey there! I'm having a little trouble trying to increase the size of my radio button using this CSS code. It seems to work in Mozilla, but not in Chrome. .rdo { margin: 1em 1em 1em 0; transform: scale(1.3, 1.3); -moz-transform: scale(1.3, 1.3); -ms- ...

Tips on using php within <<<DELIMETER:

Here is the code snippet I'm working with: $footer_rotem = <<<DELIMETER <div class="tile red"><img src="http://localhost/be/wp-content/plugins/Rotem%20Gallery/img/4.png"></div> <div class="tile red"><i ...

Does "br" not play well with flexbox?

After creating a table using flexbox, I made an interesting observation: the br element seems to have no effect within the flexbox. For example: .flexbox { display: flex; flex-wrap: wrap; border: 2px solid red; padding: 2px; } .item { width: ...

What sets apart referencing an image file in the src attribute of an img tag from directly embedding an image within an image tag?

Does the server see any distinction between using <img src=pathto.png /> and <img src=data:image/png;base64,encodedpngdata... />? If src=pathto.png is used, will the server encode the image before sending it to the browser? ...

Is there a way to display Bootstrap 4 progress bars next to each other in two distinct columns within a single container?

I am struggling with controlling the positioning of Bootstrap 4 progress bars. It seems that Bootstrap's progress bars are based on Flexbox components, which is causing confusion for me. I have provided an image showcasing my issue here Within my cur ...

Remove numerous entries from the WordPress database by selecting multiple checkboxes

A new customer table named "tblvessel" has been created in the Wordpress database. The code provided below selects records from the database and displays them as a table with checkboxes next to each record, assigning the record's 'ID' to the ...

Display the spinner until the data is successfully updated in the DOM using Angular

Dealing with a large dataset displayed in a table (5000 rows), I am attempting to filter the data using column filters. When applying the filter, I have included a spinner to indicate that the filtering operation is in progress. However, due to the quick ...

Exploring jQuery Mobile - What Causes an Empty State?

Using $.mobile.navigate("#test-page", {id:123}) for navigation to a secondary page seems to be successful. The transition between pages is smooth.... but the state remains empty! According to the documentation, the state should contain all necessary info ...

The fixed background-attachment feature does not function properly in Chrome when it is contained within a scrolling div

Essentially, if a background image is placed within a scrolling div, it will no longer remain fixed and will revert back to scrolling: CSS: <div class="wrapper"> <span></span> </div> HTML: html, body{ width: 100%; height: ...

Implement JQuery to display a loading message whenever an a tag is clicked

The website's various pages are filled with standard <a> tags, some of which have a class attribute that is utilized for styling using CSS. Each <a> tag includes a href="" attribute linking to another page on the same site. My goal is to d ...

Modifying the input value does not show changes in Firebug

Recently, I attempted to change the value of an input field using jQuery. It appeared to work, but when I checked the value of #input in Firebug, it was still showing the original value. Is this a normal behavior, or am I missing something here? Thank you ...

Issue with Laravel 5.4: AJAX behaving unexpectedly and not returning errors as it should

After going through several tutorials on handling AJAX requests in Laravel, I'm still facing some issues. Each tutorial has its own approach... Finally, one method is not giving me a 500 error, but it's not displaying validation errors as expect ...