How do you retrieve specific colored elements from an array using jQuery (or plain JavaScript)?

I am currently in the process of creating interactive checklists.

One feature I have implemented is changing the color of a header if all checkboxes associated with it are checked, as shown below:

$("input[type='checkbox']").click(function(){
if(($(this).attr('name'))=="check1"){
    maxchecked1 = $('[name="check1"]').length;
    curchecked1 = $('[name="check1"]:checked').length;
    console.log("First check"+curchecked1+"/"+maxchecked1);
    if(maxchecked1==curchecked1){
        console.log("all checked");
        $("#head1 a").addClass('ccomplete');
    }else{
        $("#head1 a").removeClass('ccomplete');
    }
}

The corresponding CSS code is:

 .ccomplete{
    background:#66BC29 !important;
  }

This functionality is working perfectly.

In addition to this, I would like to perform a similar test on elements that change color, in order to indicate when ALL the checklists are complete.

To achieve this, I attempted the following:

var heads_done  = $(".checkhead a").css("background-color" , "#66BC29");
var heads_total = $(".checkhead a").length;

The goal is to compare 'heads_done' with 'heads_total.'

However, the initial line of code seems inaccurate and I am struggling to find a solution. Essentially, I need to determine the number of elements in the array .checkhead[] (similar to check1) that have the new color.

Is there a way to accomplish this task?

Answer №1

Verifying a color may not always be reliable since browsers might not consistently return the hex value. Currently, you are setting the color instead of retrieving it. It would be more effective to check for the class instead.

let headsDone  = $(".checkhead a.ccomplete").length;
let headsTotal = $(".checkhead a").length;

if (headsDone === headsTotal) {
    alert('all tasks completed');
}

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

How to create multiple open dropdown menus in Bootstrap 4 navbar without using the toggle

Currently, I am utilizing Bootstrap 4 along with a multi-level vertical sidebar menu. An issue that I am facing is that when a dropdown item is clicked, the previously opened dropdown automatically closes. However, I would like to allow users to keep multi ...

Display a list with a set limit and an option to show more by clicking

I am currently struggling to develop a list of items that displays 3 items per row, and when the "show more" button is clicked, another 3 items should be displayed. My issue arises when using map and slice, as this method results in rendering 3 of the same ...

Deactivate the submit button when the form is not valid in angularjs

I am currently facing a challenge with my form that contains multiple input fields. To simplify, let's consider an example with just two inputs below. My goal is to have the submit button disabled until all required inputs are filled out. Here is wha ...

Using JavaFX to create a StackedAreaChart and implementing the setLowerBound method

I am encountering some issues with the Class StackedAreaChart and setLowerBound function. When I enable the autoRangingProperty to true, everything works fine. However, when I disable autoRanging and apply certain parameters, I encounter some bugs. imp ...

How to perfectly align responsive email on iPhone 6

My email design appears aligned to the left on iPhone 6 even though I have used align="center" on the tables and TD's. In my media queries, I have specified a width of 320px with the main table set to width="100%". @media only screen and (max-device- ...

Transform form data into a specialized JSON structure

I have a form set up in the following way: <form id="myForm" ng-submit="submitForm()"> <select name="ItemName" ng-controller="ItemController"> <option value="" selected disabled>Select</option> <option ng-rep ...

Struggling to implement a Rock Paper Scissors game using HTML, CSS Bootstrap4, and JavaScript, specifically facing challenges in getting the function to select a new image

Recently, in my coding class, we were tasked with creating a program that would display images of dice and generate random numbers when the refresh button was clicked. To practice using querySelector and setAttribute, I decided to expand on the lesson by i ...

Using Javascript to dynamically add variables to a form submission process

Looking to enhance my javascript skills, I've created a script that locates an existing id and exchanges it with a form. Inside this form, I'm aiming to incorporate javascript variables into the submit url. Unsure if this is feasible or if I&apo ...

Modify the class of a div as you scroll through the page between anchor points

I have been working on implementing a feature for my website where when I scroll to an anchor, the corresponding menu item adds or removes a specific class. However, I am facing an issue with making the first menu item remove the class when scrolling to t ...

Experiencing issues with applying bootstrap style to pagination when using react-js-pagination

I am currently utilizing the react-js-pagination library to showcase page numbers within my application. Bootstrap has been included in the public/index.html file and can be accessed using className in other components. When it comes to the Pagination com ...

Clearing all data in a form upon opening

Within a single portlet, I have organized two forms under separate tabs. What I am aiming for is that whenever I switch to tab 2, all fields within its associated form should automatically be cleared without the need for a button click. Even after attempti ...

issue with mark.js scrolling to selected sections

Our document searching functionality utilizes mark.js to highlight text and navigate to the results. You can see an example of this in action here. If you search for "Lorem ipsum" -> the highlighting works perfectly, but the navigation jumps to fragmen ...

Simplify your bootstrap Input field code by utilizing components or a similar method in Vue.js

Using a single file component with a pug template, I am faced with multiple input fields that have the same formatting. Here is an example: .input-group.input-group-sm .input-group-addon Purchase Price input.form-control(v-model='purchase_ ...

Exploring the world of design with React JS and MUI's diverse styling options

Exploring the various styling options offered by MUI From useTheme, styled, makeStyles to other methods - what sets them apart and how do they differ in use cases? We're looking for a comprehensive breakdown of their unique features, practical appli ...

Is there a list (collection) feature in nodejs similar to that in Java?

Is there a list collection type concept in nodejs similar to dynamic arrays? I need this kind of dynamic array functionality for my nodejs coding. Are there any concepts such as list or something similar available in nodejs? My code snippet is provided ...

Transferring Data from Python Script to Browser (with an xserver running on a Linux system)

Looking for suggestions on how to efficiently transfer data from a Python script to a web browser. The Python script, as well as the browser, are operating under an xServer environment in Linux (specifically Raspbian on Raspberry Pi). The script is respon ...

What's the reason that app.use(app.static(...)); isn't functioning properly?

As someone who is new to programming, I am currently exploring Javascript, node.js, and express. I came across a question regarding the usage of app.use(express.static(path.join(...))); versus app.use(app.static(path.join(...)));. const app = express(); co ...

The true height is never fully accurate when a vertical scrollbar is visible

Having an issue that needs solving. I've created a simple layout, but I'm struggling with getting the sidebar and wrapper to adjust their height properly. This is how the layout looks: <div class="sidebar collapse"> <div class="sideb ...

Evaluation of Library (VueJS) - Displaying various components in an individual test case

Just starting out with testing and have a simple question: I am working on testing a checkbox component. I understand the basics, but how can I render multiple components within one it block? Here is my current code. I am stuck on the second test where I ...

Ways to randomize an array without any repeating elements with the help of jQuery?

let numbers = [5, 12, 18, 23, 30]; What is the most efficient way to randomize the order of these numbers in a new array without any duplicates? Example: newArr = [18, 30, 5, 12, 23]; ...