Is there a way to tally up the number of green items and display a <p> tag when every item has been marked green?

I have created a checklist that includes checkboxes. I am looking to display some text when all the checkboxes are checked and green. Can someone assist me with writing the code for this functionality?

$(document).ready(function() {
  $("i").click(function() {
    $(this).css("color", "#5c8c44");
    $(this).parent().css("font-weight", "bold");
  });

  $("#reset").click(function() {
    $("i").css("color", "red");
    $("li").css("font-weight", "normal");
  });
});
ul li {
  list-style: none;
}

ul li i {
  color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.2/css/all.css">

<h2>Checklist</h2>
<ul>
  <li><i class="fas fa-check-square"></i> Item 1</li>
  <li><i class="fas fa-check-square"></i> Item 2</li>
  <li><i class="fas fa-check-square"></i> Item 3</li>
  <li><i class="fas fa-check-square"></i> Item 4</li>
  <li><i class="fas fa-check-square"></i> Item 5</li>
  <li><i class="fas fa-check-square"></i> Item 6</li>
  <li><i class="fas fa-check-square"></i> Item 7</li>
  <li><i class="fas fa-check-square"></i> Item 8</li>
  <li><i class="fas fa-check-square"></i> Item 9</li>
  <li><i class="fas fa-check-square"></i> Item 10</li>
</ul>
<button id="reset">Reset checklist</button>

<script src="https://code.jquery.com/jquery-3.4.1.js"></script>

Answer №1

You can find the solution here or here

$(document).ready(function() {
  $("i").click(function() {
    $(this).css("color", "#5c8c44").addClass("grencolor")
    $(this).parent().css("font-weight", "bold");
    ChkList = $(".grencolor").length;
    $("#count1").text("").append("you've marked (" + ChkList + ")/10 boxes");
    if ($("li").length == ChkList) {
      $("#count").text("").append("you've got everything!")
    } else {
      $("#count").text("").append(" you don't have all the items")
    }
  });

  $("#reset").click(function() {
    $("#count").text("").append("0")
    $("i").removeClass("grencolor")
    $("i").css("color", "red");
    $("li").css("font-weight", "normal");
  });
});
ul li {
  list-style: none;
}

ul li i {
  color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.2/css/all.css">

<h2>Checklist</h2>
<p id=count1></p>
<ul>
  <li><i class="fas fa-check-square"></i> Item 1</li>
  <li><i class="fas fa-check-square"></i> Item 2</li>
  <li><i class="fas fa-check-square"></i> Item 3</li>
  <li><i class="fas fa-check-square"></i> Item 4</li>
  <li><i class="fas fa-check-square"></i> Item 5</li>
  <li><i class="fas fa-check-square"></i> Item 6</li>
  <li><i class="fas fa-check-square"></i> Item 7</li>
  <li><i class="fas fa-check-square"></i> Item 8</li>
  <li><i class="fas fa-check-square"></i> Item 9</li>
  <li><i class="fas fa-check-square"></i> Item 10</li>
</ul>
<p id=count></p>
<button id="reset">Reset checklist</button>

<script src="https://code.jquery.com/jquery-3.4.1.js"></script>

Answer №2

One way to track the number of green buttons is by implementing a counter variable. Start by initializing the count variable, for example, var count = 0;. Then, every time a button changes to green, increment the counter. When the reset button is clicked, remember to reset the count as well. Once the count reaches a specific number, you can execute your desired actions:


var count = 0;

$("i").click(function() {
    $(this).css("color", "#5c8c44");
    $(this).parent().css("font-weight", "bold");

    count += 1;
    if(count > 5) { /*...*/ }
});

// Additional code can be added here

Answer №3

If you want to count the child elements of ul, I have made some changes to your code to achieve this. You can see the updated code and functionality here. A new function has been added to check for the green color class.

$(document).ready(function() {
  $("i").click(function() {
    //$(this).css("color", "#5c8c44");
    $(this).addClass('green')
    $(this).parent().css("font-weight", "bold");
    check();
  });

  $("#reset").click(function() {
    $("i").css("color", "red");
    $("li").css("font-weight", "normal");
    document.getElementById('output').innerHTML = '';
  });

  function check() {
    let ul_li_count = document.getElementById('ul_class').childElementCount;
    var numItems = $('.green').length;
    //console.log(numItems);
    if (numItems == ul_li_count) {
      let output = `<span> All the checkboxs are Clicked </span>`;
      document.getElementById('output').innerHTML = output;
    }
  }


});
ul li {
  list-style: none;
}

ul li i {
  color: red;
}

.green {
  color: #5c8c44
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://use.fontawesome.com/releases/v5.8.2/css/all.css" rel="stylesheet">

<h2>Checklist</h2>
<ul id="ul_class">
  <li><i class="fas fa-check-square"></i> Item 1</li>
  <li><i class="fas fa-check-square"></i> Item 2</li>
  <li><i class="fas fa-check-square"></i> Item 3</li>
  <li><i class="fas fa-check-square"></i> Item 4</li>
  <li><i class="fas fa-check-square"></i> Item 5</li>
  <li><i class="fas fa-check-square"></i> Item 6</li>
  <li><i class="fas fa-check-square"></i> Item 7</li>
  <li><i class="fas fa-check-square"></i> Item 8</li>
  <li><i class="fas fa-check-square"></i> Item 9</li>
  <li><i class="fas fa-check-square"></i> Item 10</li>
</ul>
<div id="output"></div><button id="reset">Reset checklist</button>

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

Encountering an issue of duplicate key error when using multiple v-for loops with various keys

I've encountered an issue while working on a Python application that utilizes Vue.js. A ticket came my way with an error message stating: [Vue warn]: Duplicate keys detected: ''. This may cause an update error. (found in Root) The pro ...

Removing a selected row from a data table using an HTTP request in Angular 2

I am working with a table that has data retrieved from the server. I need to delete a row by selecting the checkboxes and then clicking the delete button to remove it. Here is the code snippet: ...

Creating a double-layered donut chart with Chart.js

I'm attempting to create a unique pie chart that illustrates an array of countries on the first level and their respective cities on the second level. After modifying the data in a JSON file to align with my goal, it doesn't seem to be working a ...

Error occurred in next.js environment file when referencing process.env keys as strings in .env.local file

I have a .env.local file with various values stored in it. NEXT_PUBLIC_GA_ID = myvariablevalue I created a function to validate the presence of these values: export const getEnvValue = (name: string, required = true) => { const value = process.env[na ...

Looking to obtain the coordinates of a draggable element?

After dragging a div around the page and submitting a form, I would like to capture its location on the page so it can render in that same spot when the page reloads. My current question is how can I capture the coordinates or location of the div after it ...

Guide on uploading files using Vue.js2 and Laravel 5.4

I'm currently attempting to implement an image upload feature using Laravel for the backend and Vue.js2 for the frontend. Here are snippets from my code: addUser() { let formData = new FormData(); formData.append('fullname', this.n ...

Encountering a problem while creating a Page Object in webdriver.io - getting the error "setValue is not a function" or "cannot read property 'setValue' of undefined"

While working on a webdriver.io automation project, I encountered an issue with recognizing objects in my page object file (login.po.js) when calling them in the test spec file (test.spec.js). The error message displayed is LoginPage.username.setValue is n ...

Getting the chosen option from a dropdown list mapped in ReactJS

I am working on a dropdown select option that is linked to the data of an array object called 'template_titles'. Currently, the value in the dropdown corresponds to the title in the object. My goal is to be able to extract and use the selected va ...

The assets path is the directory within the installed package that houses the main application files following the completion of a

I have a Vue.js UI component that is internally built using webpack. This reusable UI component library references its images as shown below: <img src="./assets/logo.png"/> <img src="./assets/edit-icon.svg"/>   <i ...

ajax [rails] allows for the entire page to be updated without a full reload

My task should be fairly straightforward: I have a page that displays all records if the request was made using http, and retrieves records between dates if it was an ajax request. Here is how the page looks This is index.html.erb <h1>Jobs History& ...

Tips for displaying an edit action icon when hovering over specific text

Looking for a way to display or hide the edit icon when hovering over specific text? Take a look at this snippet of HTML code: <ul> <li> <a id="pop" href="javascript:;;" data-content="test Desc" data-id="123"> &l ...

What is the purpose of requiring the explicit invocation of app.listen(port) to enable express-ws to function properly?

I've recently started exploring NodeJS Express and came across the official tutorial from express-ws for setting up websockets in a simple project generated using npx express-generator. While following the tutorial, I noticed that in the app.js file, ...

Receiving HTTP POST data using Classic ASP script

I'm currently working on a project and have come across an area where I am facing some challenges. Despite my best efforts, I haven't been able to find a solution using Google. In my ASP web application, I've added an HTML canvas that I nee ...

Steps for importing JQuery into a custom DNN module

I have developed a custom DNN module that requires the use of certain JQuery plugins. I successfully loaded the plugins, but encountered the following error: Uncaught TypeError: undefined is not a function imagesloaded.js?cdv=18:93 Uncaught TypeError: ...

The chat text will automatically scroll down, displaying information loaded from the database

Is there a way to ensure that chat text always remains scrolled to the bottom of the chat? I have tried multiple tutorials and examples from Stack Overflow, but none seem to work for me. Currently, my chat text overflows the textarea when there are too m ...

Sending data between two elements when a jQuery event is triggered

As a JavaScript beginner, I am facing an issue where I need to push data from an h1 tag to a textarea. My website is built using WooCommerce and when a visitor clicks on a product, a chat box with the product title opens. Currently, I have successfully p ...

Is it possible to use jQuery validate for remote parsing with two fields in a single call

Currently, I am facing an issue while trying to parse two values using jQuery's validate plugin to compare with an SQL database. The DateReceived value is successfully parsed, but the CentreID value always appears as null. Below is the code snippet I ...

Angular has the feature of a right float button with *ngfor

I've successfully implemented a form using Reactive Forms in Angular. Currently, my form is displayed as follows: <div class="center" appMcard> <form [formGroup]="GroupRMPM_FG"> <div formArrayName="GroupId_Name" *ngFor="let ...

Utilizing Twitter API authentication to prevent hitting rate limits

Currently, I am implementing the Twitter API to showcase the most recent tweets from 4 distinct users on my webpage. It seems that once a certain number of calls are made, an error message appears stating "NetworkError: 400 Bad Request" and prevents the tw ...