Could a css style be applied to a parent element based on the status of its child element?

In my specific context, this is the HTML code I have:

<div class='table'>
      <div>
          <div *ngFor='let product of this.market[selectedTab].products | orderBy:"id"' class='itemlist'  [ngClass]="{'active': isAdded}">
            <div class='child'>
              <div class='first'>
                <div>
                  <h2>{{product.name}}</h2>
                  <ul class='subtitle' *ngIf='this.selectedTab === 0'>
                    <li class='itemsdesc' *ngFor='let item of product.items'>
                      <div>
                        <p>{{item.name}}</p>
                        <p>{{item.description}}</p>
                      </div>
                    </li>
                  </ul>
                  <p [innerHTML]='product.description' class='box-desc' *ngIf='product && product.description'></p>
                </div>
              </div>
            </div>
            <div class='child'>
              <div class='fourth'>
                <ul class='p-0 m-0'>
                  <li *ngFor='let item of product.items' class='def-number-input number-input d-flex justify-content-center'>
                    <a (click)='minus(item)' class='minus'[ngClass]="{'disable': isMinor}">-</a>
                    <input class='quantity' type='number' placeholder='0' [(ngModel)]='item.quantity'>
                    <a (click)='plus(item)' class='plus'>+</a>
                  </li>
                </ul>
              </div>
            </div>
            <div class='child'>
              <div class='fifth'>
                <ul class='p-0 m-0'> 
                  <li *ngFor='let item of product.items; let i = index''>                
                    <button class='cart' [ngClass]="{'disable': isUndefined(item.quantity) || item.quantity === 0}" (click)='this.updateCart(item); this.getTotalItems()'><i class='icon-addcart'></i></button>
                  </li>
                </ul>
              </div>
            </div>
          </div>
        </div>
      </div>

According to my interactions, when I click on the button with the 'cart' class, I change the color of the button itself and I want to add a border to the div with the 'itemlist' class, so I activate an ngClass named 'active'. The issue arises when the border gets activated for all the 'itemlist' items on the screen instead of just the one that meets the condition in the button with the 'cart' class.

As you can see from my HTML, I tried to modify it using a class variable 'isAdded', but it affects all the 'itemlist' elements. I also attempted to change the condition of the [ngClass] based on the item.quantity, but at that point, it returns the value as undefined.

So, the question here would be whether there is a way to detect changes in the item quantity at the level of the parent component with the 'itemlist' class in order to modify its class?

Thank you in advance for your assistance

Answer №1

To resolve the issue, I included a new property 'isAdded: false' in the original data load. Subsequently, I created a method to check if the item's id has been selected and then update the class accordingly.

  isProductItemAdded(argproduct) {
    const products = this.market[this.selectedTab].products;
    const product = products.find(p => p.id === argproduct.id);
    const isAdded = product.items.some((i: any) => i.isAdded);
    return isAdded;
  }

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

Creating uniform line lengths with a ruler utilizing Fabric.js

I have a div that scrolls horizontally and contains a ruler div and a canvas where I draw horizontal lines of varying lengths. When drawing the lines, I want to ensure they are accurately measured against the ruler using JavaScript and CSS: var canvas = ...

Using CSS to style the last element in a set of dynamic content

I am currently working on targeting the last element in a dynamically generated content set. For instance, here is an example of how the generated content appears: <div class="block"> <div class="block-inner"> <div class="box"> ...

The functionality of the "#Next" and "#Prev" features in the Tiny carousel are currently not functioning properly within the MVC project

Looking to implement a simple content carousel using jQuery 2.1.3 in my project. Previously used a non-responsive jQuery content carousel and switched to find a responsive option:Tiny Carousel Added Tiny Carousel's .js and .css files to Bundles.confi ...

How can one safeguard their JavaScript code from potential threats and unauthorized access?

How can I ensure that a web app is not accessible or operated from the local file system? The app has a custom front-end workspace created with JS and various libraries, which should only be usable when the user is logged in to the domain with an active i ...

Navigational dropdown menu in Bootstrap

I'm a bit rusty with Bootstrap and I'm struggling to make my NavBar dropdown toggle. I've gone through all the documentation and forums available but can't seem to figure it out. Any ideas on what I might be missing? <nav class=" ...

Footer at the bottom and a sticky navigation bar

I have experimented with various CSS guides in order to place the footer at the bottom of the page, and I have found the example below to be the simplest method. html, body, #root { height: 100%; } #root { display: flex; flex-direction: column; } ...

HTML files do not inherit from other HTML files

I am venturing into the world of VSCode for the first time to create a web application. Starting with the basics, I have an index.html page with a simple "Hello, world!" message. Additionally, I have developed a layout.html file that contains code for a na ...

Stop the endless scrolling of the carousel and keep it fixed in place

I'm having trouble with disabling auto scrolling even though I've set bs-interval to "false". Here's my code snippet: <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfem ...

Display or conceal several elements using JQUERY/HTML upon hovering

Here is the current progress: <div style="position: relative;"> <a href="#games"> <div class="sidenavOff"> <img src = "images/card_normal.png" /> <img src = "images/category_icons/icon_games.png" style = "position: a ...

Layering an object on top of a clone using CSS

I am working on a webpage with a jQuery menu located at the following link: Menu To accommodate more items and have greater control, I duplicated the menu twice. However, when hovering over them, the duplication is visible in the background. Is there a ...

What steps should I take to correct the alignment of this grid using CSS?

I'm currently working on creating a grid layout for my website and I've almost achieved the desired result. However, I'm facing an issue with the size of the "Projects" title within the grid. I want it to stand out and be larger compared to ...

Adjust image size dynamically while keeping the original aspect ratio

Is there a way to scale variable portrait and landscape images dynamically to fit proportionally within a browser window? You can find my current image resizing attempt here: http://jsfiddle.net/6pnCH/4/ I would like the image to be already scaled vertic ...

"Troubleshooting problems with jQuery accordion collapse and styling using CSS

I'm currently dealing with an issue regarding my accordion design. I have customized the stylesheet using themeroller, but when I collapse one of the sections, the header changes to black instead of reverting back to its original red color. I've ...

Verify the fonts that are functioning correctly

Despite using the "dancing script" Google webfont and adding it through @font-face, font-family, I am unable to see it in the browser. I would like to know how to determine which fonts are correctly uploaded and rendering properly while viewing a website ...

Attempting to toggle the visibility of div elements through user interaction

I'm having an issue with click events on several elements. Each element's click event is supposed to reveal a specific div related to it, but the hidden divs are not appearing when I click on the elements. Any help in figuring out what might be g ...

Highlighting table rows on mouseover using CSS

I am trying to add a hover effect to my table rows, but when I apply the CSS styles for this behavior, the rows start wrapping. Here's a comparison of how the table rows look before and after applying the styles. You can see the wrapping issue in the ...

Initial space in model variable not being displayed

Hey there, I am working with a model variable called "name" that is linked to a span element like this: <input type="text" ng-model="name"> <span ng-bind="name"></span> My goal is to display the text entered in the input field without r ...

Tips for creating a responsive fixed div/nav that adjusts its size based on the container it is placed within

Just starting out in web development and struggling with this issue. Any assistance would be much appreciated! When resizing the window, the fixed div moves outside of its container instead of resizing. The navigation bar on the website I'm working o ...

Is there a way to program custom key assignments for my calculator using JavaScript?

As a beginner in the world of coding and JavaScript, I decided to challenge myself by creating a calculator. It's been going smoothly up until this point: My inquiry is centered around incorporating keys into my calculator functionality. Currently, th ...

The issue of the footer kicking out of place in the HTML layout was successfully resolved after

I am in the process of creating a simple footer that will always stay at the bottom of the page. The content on the page will not exceed the screen size, eliminating the need for scrolling. (There are a total of 5 HTML pages, and I want the footer and head ...