Implementing a tracking mechanism

My goal is to incorporate a counter into this feature. When a number is clicked, the message should read "You've selected 1 out of 5," indicating that the user needs to choose 5 numbers. If the same number is clicked again, it should deselect and revert to displaying "You've selected 0 out of 5." So, for example, the user could click on numbers 1, 5, and 7, and then the message should update to "You've selected 3 out of 5."

<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <title>UniqueConcepts</title>
      <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
      <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
      <style>
         #feedback {
         font-size: 1.4em;
         }

         #selectable .ui-selecting {
           background: #FECA40;
         }

         #selectable .ui-selected {
           background: #F39814;
           color: white;
         }

         #selectable {
          list-style-type: none;
           margin: 0;
           padding: 0;
           width: 450px;
         }

         #selectable li {
         margin: 10px;
         padding: 1px;
         float: left;
         width: 100px;
         height: 80px;
         font-size: 4em;
         text-align: center;
         }

         .wrapper {
           display: flex;
           margin: auto;
           justify-content: center;
           align-items: center;
         }
      </style>
   </head>
   <body>
      <div class="wrapper">
         <div>
            <p id="feedback">
               <span>You've selected:</span> <span id="select-result">none</span>.
            </p>
            <ol id="selectable">
               <li class="ui-widget-content" text="car">1</li>
               <li class="ui-widget-content" text="truck">2</li>
               <li class="ui-widget-content" text="physics">3</li>
               <li class="ui-widget-content" text="maths">4</li>
               <li class="ui-widget-content" text="home automation">5</li>
               <li class="ui-widget-content" text="car6">6</li>
               <li class="ui-widget-content" text="car7">7</li>
               <li class="ui-widget-content" text="car8">8</li>
               <li class="ui-widget-content" text="car9">9</li>
               <li class="ui-widget-content" text="car10">10</li>
               <li class="ui-widget-content" text="car11">11</li>
               <li class="ui-widget-content" text="car12">12</li>
            </ol>
         </div>
      </div>
      <script>
         // This allows each box to be clickable 
         let liSelected = [];
$("document").ready(function() {
    $(".ui-widget-content").on("click", function() {
        if($(this).hasClass('ui-selecting')){
            $(this).removeClass('ui-selecting');
            removeItem(liSelected, $(this).attr('text'));
        }else{
            $(this).addClass('ui-selecting');
            liSelected.push($(this).attr('text'))
        }
        $("#select-result").text(liSelected.length === 0 ? "none" : liSelected.join(", "));
    });
// Check for existing title in array and remove it
    function removeItem(array, item){
        for(var i in array){
            if(array[i]==item){
                array.splice(i,1);
                break;
            }
        }
    }
});
      </script>
   </body>
</html>

Answer №1

Instead of showing the array of selected items, you should now display the total number of selected items.

All you need to do is update

$("#select-result").text(liSelected.length === 0 ? "none" : liSelected.join(", "));

to

$("#select-result").text(liSelected.length === 0 ? "none" : (liSelected.length + ' out of 5'));

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 customize a button component's className when importing it into another component?

Looking to customize a button based on the specific page it's imported on? Let's dive into my button component code: import React from "react"; import "./Button.css"; export interface Props { // List of props here } // Button component def ...

Creating subpages using IDs can be accomplished by following these simple steps

Currently, I am in the process of developing a website that contains a plethora of information, specifically news articles. Each news article on my site features an introduction and a header. Upon clicking on a particular news article, the full content is ...

Pressing the follow button does not save data in the database

Whenever I click on a follow button, it should store followerid and followingid in the follow table and change to an unfollow button. If I then click on the unfollow button, those entries should be deleted from the follow table. I have used JQuery for this ...

NextJS 12: The dilemma of styled components not rendering server side due to missing CSS

Exciting news with the latest NextJS 12 release - now styled-components is supported without the need for any extra plugins on NextJS! However, I'm running into issues getting it to work with server-side rendering. To activate it, I installed the sty ...

Execute the function at the top of every hour

I need to trigger a jQuery function on the dot of every hour, like at 11:00, 12:00, 13:00 and so on. Below is the function I want to call: function flipIt() { $('#flip').addClass('animate pulse'); $('#flip').removeCl ...

What is the best method for storing numerical data for a Next.js/React website? Should you use a CSV file, make a backend API call, or download

I'm working on a nextjs website and I want to integrate a chart. Where would be the best place to store the data for this chart? Here are some options I've considered: Save a csv file in the public folder and retrieve it from there Store a csv f ...

Jasmine's capability to test method logic that executes following the resolution of a returned promise

I am looking to test a JavaScript method that I have written. Here is a simplified pseudo code representation of it: $scope.savePerson = function(){ Person.create($scope.person).then(function(newPerson){ if($scope.person.org){ ...

Uploading data using jQuery and submitting it using AJAX techniques

I am currently working on uploading and posting data to a modal window. Although my code works perfectly in IE 10, Chrome, Firefox, Opera, it seems to have some issues in IE versions below 10. Does anyone have any ideas on how I can fix this "bug" or what ...

The background color of the div or section is stubbornly refusing to change, no matter how much I tweak it

.forBackground { background-color: white; } .kontaktUndMapContainer { width: 100%; padding: 20px; color: gray; text-align: center; } .überschriftKontakt { margin-bottom: 20px; } .überschriftKontakt2 { margin-top: 20px; margin-bottom: 2 ...

Numerous unique designs paired with exclusive paths

I am working on setting up a system that accommodates multiple layout variations and includes private routes to display specific content based on the user's login status and assigned layout. Currently, I have three different layouts in place, with the ...

What causes an error in RSVP Deferred when a promise is invoked multiple times?

What causes an error in RSVP Deferred when the promise is called twice? There appears to be a distinction between deferred.promise.then().finally() and deferred.promise.then(); deferred.promise.finally(). Why is this? RSVP.on('error', functio ...

Example of Utilizing Google Places API

The Issue I've been struggling to make this google maps API example function correctly. Even after directly copying the code from Google's website, the example fails to display properly. For instance, the map doesn't show up, the search bar ...

By default, have jQuery disabled input fields

I am looking to set the inputs to be disabled by default, and I have created a checkbox that will enable them when checked. Here is the code for the checkbox functionality: function toggleStatus() { if ($('#toggleElement').is(':checked&ap ...

What is causing my mobile version not to resize and adjust to fit the screen properly?

Currently utilizing Bootstrap 4 along with the viewport meta tag, %meta{:content => "width=device-width, initial-scale=1, shrink-to-fit=no", :name => "viewport"}/ Even after hiding all images with .hidden-xs-up, the display on Chrome mobile appear ...

What is a way to retain the value of a variable after a request, while starting off with a different value on the initial load?

In my Flask application, users have the option to choose a specific time period with a start date and an end date. When the page initially loads, I want the start date to default to the first day of the current month and the end date to be the current day. ...

Aligning vertical pills to the right using Bootstrap framework

Here is the relevant code snippet that I am working with: <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <!-- jQuery library --> <scrip ...

Tips for overlaying text on the background in Next.js:

I'm currently working on creating an image element with overlay text. Check out my jsx code below: <div className={styles.img}> <img src={src} alt="" /> <p>{`(${size})`}</p> </div> And here is t ...

When you hover over the vertical sidebar menu in Bootstrap material design, a submenu will appear

Looking to create a three-column display that functions as one submenu when hovering over each menu item? <div class="container"> <div class="row"> <div class="col-sm-4"> <ul class="vertical-nav"> <li>&l ...

Effective methods to prevent the `translate` transform from triggering an element to be perceived as position relative?

I am encountering an issue with an absolutely positioned div that is nested within a statically-positioned parent container. I noticed that when I apply the transform: translateY(-50%) property to the container, the child element behaves as if it were bein ...

What is the best way to horizontally align text in a container without it remaining at the top?

My attempts to center the button using vertical-align: middle and text-align: center have been unsuccessful, as the styling does not affect the button's position and it remains at the top. I am puzzled by why the style is not being applied. While I c ...