Ways to adjust the color of a div

I'm currently working on an app using Angular.js and HTML, but I've hit a roadblock.

There's a dropdown at the top, and when it changes, I want to adjust the background color of certain cells as shown in this image: https://i.sstatic.net/OvhPH.png

Below is a snippet of my code:

HTML page

<div class="row">
        <div class="col col1" style="-webkit-box-shadow: 0 0 20px 0px #999;background-color: #11c1f3;text-align: center;">
            <h3 style="color: white">Time Slot</h3>
        </div>
        <div class="col col1" ng-repeat="time in timeArray" style="background-color: #e0e0e0;text-align: center">
            {{time}}
        </div>
    </div>
    <!-- <div id="selectedDateRange"> -->
    <div class="row" ng-repeat="(dateindex, date) in dateArray">
        <div class="col col1" style="-webkit-box-shadow: 0 0 20px 0px #999;background-color: #11c1f3;text-align: center;">
            <h3 style="color: white">{{date}}</h3>
        </div>
        <div class="col col1" ng-repeat="(timeindex, time) in timeArray" ng-click="selectCell(dateindex, timeindex)" ng-class="{'selected': selectedCell[0] == datein
dex && selectedCell[1] == timeindex}"></div>
    </div>

Controller.js code

Here is the click event for the dropdown change:

    $(function () {
            $('#seldate').change(function () {

            //I need to change the color of the cell in the first row and first column
           //also, the color of the cell in the second row and second column should change
           })

         })

Any suggestions on how to change the background color of a cell?

Your help would be greatly appreciated! Thank you in advance!!

Answer №1

Implement the track by $index in your ng-repeat directive.

Then, add a style attribute to the cell like this:

color : $index === 1 ? 'red' : 'green'

You can also explore using ng-style

Answer №2

If you want to update your content, consider implementing this code snippet (you can customize it further based on your needs)

<div class="row">
  <div class="col col1" style="-webkit-box-shadow: 0 0 20px 0px #999;background-color: #11c1f3;text-align: center;">
    <h3 style="color: white">Time Slot</h3>
  </div>
  <div class="col col1" ng-repeat="time in timeArray" style="background-color: #e0e0e0;text-align: center" ng-change="currentColor = time.color">
    {{time}}
  </div>
</div>
<!-- <div id="selectedDateRange"> -->
<div class="row" ng-repeat="(dateindex, date) in dateArray">
  <div class="col col1" style="-webkit-box-shadow: 0 0 20px 0px #999;text-align: center;">
    <h3 style="color: white">{{date}}</h3>
  </div>
  <div class="col col1" ng-repeat="(timeindex, time) in timeArray" ng-click="selectCell(dateindex, timeindex)" ng-class="{'selected': selectedCell[0] == dateindex && selectedCell[1] == timeindex}"  ng-style="{'background-color':currentColor}"></div>
</div>

To change the background color of a specific cell, consider setting a boolean or logic for that cell

Answer №3

My preferred approach when handling situations like this involves adding an id element to the item that is being repeated. Then, in the onclick function, you can pass the element just as you have done. After that, you have the flexibility to modify it using jQuery or any other preferred method.

<div class="row" ng-repeat="(dateindex, date) in dateArray">
  <div class="col col1" style="-webkit-box-shadow: 0 0 20px 0px #999;text-align: center;">
    <h3 style="color: white" id="dateArray_{{dateindex}}>{{date}}</h3>
  </div>
  <div class="col col1" ng-repeat="(timeindex, time) in timeArray" ng-click="selectCell(dateindex, timeindex)" ng-class="{'selected': selectedCell[0] == dateindex && selectedCell[1] == timeindex}"  ng-style="{'background-color':currentColor}"></div>

In your controller code:

$scope.selectCell = function(dateindex, timeindex){
  $("#dateArray_" + dateindex)
  .css({
    'color' : 'green'
  });
}

Answer №4

Utilize ng-class in AngularJS to dynamically apply CSS properties

For example:

<p ng-class="bold:isBold"> Applying Dynamic Properties </p>
<input type="checkbox" ng-model="isBold">make text bold

When the value of isBold is true, the bold property will be applied

Pro Tip: You can apply multiple properties using ng-class

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

What is the best way to incorporate multiple calculations in JavaScript?

As a newcomer to JavaScript, I am trying my hand at creating an online tax calculator for our website. While I can understand pre-written calculations, developing my own has proven to be challenging. The objective is simple: the user inputs their Gross Inc ...

Creating dynamic tables with jquery mobile

When the screen width decreases, I need to find a way to position 3 tables below each other without using float or breaking the jquery mobile layout. Using float: left; on the surrounding containers of each table is not an option as it disrupts the jquery ...

Discovering Dynamic Field IDs in Selenium WebDriver using C#

Consider the following scenario, where different fields are displayed based on the selected report. In the Selenium code snippet provided below, the Fields are defined as: By Field1 = By.Id("ctl00_MainContent_cntrlDynamicField1"); By Field2 = By.Id("ct ...

Guide to recreating the Material Ripple effect using Vuejs

I am in the process of recreating the ripple effect inspired by Material Design for my current project. I have decided to move away from Quasar and build all the elements from scratch. Here is the effect: https://i.sstatic.net/VKJIc.gif I have seen some ...

Error in Angular form validation: Attempting to access property 'name' of an undefined value

Recently, I encountered an issue with my form while implementing Angular validation. The goal was to ensure that the input fields were not left blank by using an if statement. However, upon testing the form, I received the following error message: Cannot ...

Unable to adjust item inventory in MongoDB, utilizing Node.js and React.js

I'm currently developing a warehouse management website where I store my data in MongoDB. One of the key features I am working on is the ability to update product quantity with just a button click and have it reflected on the user interface. However, ...

The asynchronous JavaScript function is successfully printing data, however it is encountering an error where it returns 'undefined'

Struggling with asynchronous calls, I've realized this question has been answered many times before. Despite trying numerous suggested approaches without success, I would truly appreciate any help. Progress has been made recently, but I still consider ...

Utilizing Jquery to enhance table filtering functionality

I created a table listing various places, along with buttons to filter the list. The first filter is based on location (north, east, central, south, west) determined by postcode. The other filter is for "impress" which only displays places with a rating of ...

Capture an image of the video frame every second

Have you ever noticed the cool feature on YouTube where you can easily 'scrub' through video and see a thumbnail view at each second? https://i.sstatic.net/Yi8He.png I'm curious - how is this achieved? Does the server have to send over eac ...

Retrieve the element located within a "block" element that is relative to the user's click event, without the

I'm pondering whether it's feasible, but here's my concept: Within my page, there are multiple identical blocks with the same classes, differing only in content. I am unable or unwilling to assign IDs because these blocks are dynamically g ...

Increasing the name attribute with Jquery cloning

I'm interested in duplicating the addDriverContent (which is currently functional) and updating the numbers in the name attribute from name="description" to name="description2", name="description3" on the duplicates Additionally, if anyone knows how ...

Align the present domain using XPath

Is there a way, possibly with a newer version of XPath, to make the following code work: //a/@href[not contains("DOMAIN OF THE CURRENT PAGE")] The DOMAIN OF THE CURRENT PAGE should function as a variable that retrieves the domain - similar to something l ...

Changing CSS properties in React

Currently, I am working on a dynamic table component using CSS grid. My goal is to customize the grid-template-columns CSS property. I am familiar with using repeat in CSS to achieve this. However, I am facing a challenge when it comes to dynamically chang ...

What caused the auto resize feature of the Automatic Image Montage jQuery plugin to malfunction?

Apologies in advance if my issue is a bit convoluted. I've integrated the Automatic Image Montage jQuery plugin into a page I'm currently working on. However, I seem to have disrupted the functionality that automatically resizes images when the w ...

Show the response obtained after making a POST request

In my current project, I am utilizing vanilla JavaScript to send a POST request to my Flask App. I have implemented a validation feature that checks for duplicate usernames when a user is signing up. If the username already exists, a 406 status response is ...

Tips for capturing changes in a "count" variable and executing actions based on its value

I have a counter on my webpage and I'm trying to change the style of an element based on the count variable. I tried using the .change action but I haven't been able to get it working. It might not be the right solution. Can anyone provide some ...

Ways to expand HTML input fields to fill the whole table cell

Currently working on a calculator project and I'm facing an issue with making a HTML button span the entire cell. I've managed to stretch the button horizontally but struggling to get it to fill the cell completely. Curious as to why setting widt ...

Tips for incorporating the feature that enables the inclusion of an article onto a list

I am looking to incorporate the ListArticle component, which allows for adding an article to a list. import React, { useState } from "react"; export default function ListArticle() { const [id, setId] = useState(0); const [designation, setDesi ...

How can I retrieve the results of an SQLite call in HTML5 without passing them to a separate function?

How can I use SQLite in HTML5 to execute a call like the one below, so that the result is returned immediately rather than passed off to another function? try { mydb.transaction( function(transaction) { transaction.executeSql( &apo ...

Updating the title of a bootstrap using jQuery AJAX call

Is there a way to dynamically load content for Bootstrap Tooltips using AJAX? I want the data to be displayed in a tooltip when a user hovers over a button. Currently, I am utilizing jQuery 3.6.0 with the following code snippet: $('document').re ...