Setting the cursor back to default for elements that have the is-inactive class assigned

Objective

In the selection process for players, after the maximum number of goalies, defensemen, and forwards have been chosen, any remaining players who are marked as inactive should have their cursor set to default.

Clarification of the problem

By default, all players are marked as inactive, and the objective is to change the cursor behavior to default for these inactive players only after other players have been picked and switched to active status.

For example: Once two goalies are picked and marked as active with a pointer cursor on hover, the remaining eight goalies in the category should remain inactive with the default cursor behavior.

Problem

  • The issue is that even players marked as inactive still retain the pointer cursor instead of default.

style.css

.player {
  display: inline-block;
  margin-top: 15px;
  margin-right: 20px;
  vertical-align: top;
  cursor: pointer;
  position: relative;
}

index.html

<div class="player player--goalie year--1990">
  <div class="tooltip tooltip--tall">
    <p class="tooltip__name">Brian Elder</p>
    <p class="tooltip__hometown"><span>Hometown:</span> Oak Lake, Man.</p>
    <p class="tooltip__years"><span>Years Played:</span> 1992-1997</p>
    <div class="tooltip__stats--inline">
      <div class="stats__group stats--games">
        <p class="stats__header">GP</p>
        <p class="stats__number stats__number--games">110</p>
      </div>

      <div class="stats__group stats--goalsag">
        <p class="stats__header">GA</p>
        <p class="stats__number stats__number--goalsag">2.00</p>
        <p class="stats__number">3.12</p>
        <p class="stats__number">3.46</p>
        <p class="stats__number">2.70</p>
      </div>

      <div class="stats__group stats--savep">
        <p class="stats__header">SAV%</p>
        <p class="stats__number stats__number--savep">.909</p>
        <p class="stats__number">.886</p>
        <p class="stats__number">.884</p>
        <p class="stats__number">.906</p>
      </div>

      <div class="stats__group stats--shutouts">
        <p class="stats__header">SO</p>
        <p class="stats__number">0</p>
        <p class="stats__number">0</p>
        <p class="stats__number stats__number--shutouts">3</p>
        <p class="stats__number">2</p>
      </div>
    </div> <!-- tooltip__stats--inline -->
  </div> <!-- tooltip -->
  <div class="player__headshot player--elder">
    <div class="picked is-inactive"><i class="fa fa-star" aria-hidden="true"></i></div>
  </div>
  <p class="player__name">Brian Elder</p>
  <p class="player__position">Goalie</p>
</div>

scripts.js

/*-------------------------------------
COUNT SELECTED
--------------------------------------*/

function countSelected() {
    $(".player").on("click", function(){

        // Checks if the maximum number of players have been selected
        // If so, return false and then do nothing
        // If not, the class will toggle from `is-inactive` to `is-active`
        if ($(this).find(".picked.full").length > 0) return false;
        $(this).find(".picked").toggleClass("is-inactive is-active");

        // Count the number of players with stars
        var starredGoaltenders = $(".player--goalie").find(".picked.is-active").length;
        var starredDefencemen = $(".player--defencemen").find(".picked.is-active").length;
        var starredForwards = $(".player--forward").find(".picked.is-active").length;

        console.log(starredGoaltenders, starredDefencemen, starredForwards);

        // The number of starred players for each position cannot exceed the following numbers
        var maxGoaltenders = 2;
        var maxDefencemen = 6;
        var maxFowards = 12;

        // If the number of starred players hits its max, a class of `is-completed` is adding to the corresponding checkmark to indicate that the task has been completed
        if (starredGoaltenders === maxGoaltenders) {
            $(".checkmark--goalie").addClass("is-completed");
            $(".player--goalie").find(".picked").addClass("full");
        }

        if (starredDefencemen === maxDefencemen) {
            $(".checkmark--defencemen").addClass("is-completed");
            $(".player--defencemen").find(".picked").addClass("full");
        }

        if (starredForwards === maxFowards) {
            $(".checkmark--forward").addClass("is-completed");
            $(".player--forward").find(".picked").addClass("full");
        }

        // If all the conditions are met show the submit vote button
        if (starredGoaltenders === maxGoaltenders && starredDefencemen === maxDefencemen && starredForwards === maxFowards) {
            $(".btn--submit").show();
            $(".btn--submit").addClass("slideLeft");
        }
    });
} countSelected();

Answer №1

If you're open to using a JavaScript solution, one way to tackle the issue is by adding a class to the .is-inactive players after the first click of a player.

UPDATE My initial example was slightly off because the .is-inactive and .is-active classes apply to descendants, not the .player objects themselves. However, the snippet below offers the correct implementation:

SECOND UPDATE Following a discussion with the OP regarding their requirements, the following approach better aligns with their needs. I've shifted the .is-active and .is-inactive classes to the .player elements and included these lines in the click function:

$(".player").filter(".is-active").removeClass("not-picked");
$(".player").filter(".is-inactive").addClass("not-picked");

Additionally, update the CSS with:

.not-picked {
  cursor: default;
}

So now, each time a player is selected or deselected, the corresponding player will be highlighted, and the cursor will change unless the player-type (e.g., goalie) is full.

The code includes functionality for selecting players based on position and limiting the number of selected players per position. When all conditions are met, a submit button becomes visible.

Answer №2

You have the ability to target elements that have multiple classes by using .class1.class2. Consider inserting the following code at the conclusion of your CSS.

.player .is-active.picked {
  cursor: pointer;
}

Remember: It is recommended to refrain from using - in class names. Instead of is-inactive, opt for isInactive or is_inactive.

.player {
  display: inline-block;
  margin-top: 15px;
  margin-right: 20px;
  vertical-align: top;
  cursor: default;
  position: relative;
}
.player.is-inactive {
  cursor: default;
}
<div class="player is-inactive player--goalie year--1990">
  <div class="tooltip tooltip--tall">
    <p class="tooltip__name">Brian Elder</p>
    <p class="tooltip__hometown"><span>Hometown:</span> Oak Lake, Man.</p>
    <p class="tooltip__years"><span>Years Played:</span> 1992-1997</p>
    <div class="tooltip__stats--inline">
      <div class="stats__group stats--games">
        <p class="stats__header">GP</p>
        <p class="stats__number stats__number--games">110</p>
      </div>

      <div class="stats__group stats--goalsag">
        <p class="stats__header">GA</p>
        <p class="stats__number stats__number--goalsag">2.00</p>
        <p class="stats__number">3.12</p>
        <p class="stats__number">3.46</p>
        <p class="stats__number">2.70</p>
      </div>

      <div class="stats__group stats--savep">
        <p class="stats__header">SAV%</p>
        <p class="stats__number stats__number--savep">.909</p>
        <p class="stats__number">.886</p>
        <p class="stats__number">.884</p>
        <p class="stats__number">.906</p>
      </div>

      <div class="stats__group stats--shutouts">
        <p class="stats__header">SO</p>
        <p class="stats__number">0</p>
        <p class="stats__number">0</p>
        <p class="stats__number stats__number--shutouts">3</p>
        <p class="stats__number">2</p>
      </div>
    </div>
    <!-- tooltip__stats--inline -->
  </div>
  <!-- tooltip -->
  <div class="player__headshot player--elder">
    <div class="picked is-inactive"><i class="fa fa-star" aria-hidden="true"></i>
    </div>
  </div>
  <p class="player__name">Brian Elder</p>
  <p class="player__position">Goalie</p>
</div>

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

Changed over to a promise-oriented database, causing my login feature to malfunction completely

Although I can successfully register, when I am redirected to my game route, all I see is a blank Error page with [object Object] on the screen. This message also appears in my console periodically. Initially, I suspected an issue related to socket.io, bu ...

Utilizing JQuery Autocomplete to Display Names and Retain IDs with PHP and MySQL

I am facing an issue with the autocomplete feature where the list appears blank (only borders, no content). Here is the code snippet: The Data I receive : <?php require 'cnx/cnx.php'; $stmt = $pdo->prepare('select * from auteurs w ...

Removing elements from an array with reduced speed

Within my array, I am looking to remove N elements from the beginning. For instance, if my array contains 1 million floating point elements and I need to remove the first 500,000, I have two options. The first is to iterate and call the shift method 500,0 ...

Inspect the key within a complex hierarchy of nested objects in an array

I am working with an array of nested objects and need to extract the value of a specific key property. Currently, I am using a for loop and checking for the existence of children property, but I feel there might be a more optimal way to accomplish this tas ...

Enable/Disable Button Link Tag

Utilizing AJAX, I have successfully managed to change the flag status to Active or Deactive. My goal now is to disable the button if the flag status is set to 1 or 0. However, I am facing an issue with the 'if clause' as the entire button is bein ...

Is it possible to close the navigation menu by clicking on a link or outside of the navigation area?

Hey everyone, I've recently dived into the world of web design and encountered my first hurdle. I need your expertise to help me solve this problem. How can I modify my JavaScript to close the NAV element by clicking on one of the links or outside t ...

Fixing perspective clipping in Three.js

In my Three.js project, I have a plane inside a sphere that I am applying a shader to in order to achieve certain visual effects on the sphere. To ensure that the plane is always facing the camera, I am using the lookAt method. However, I have noticed that ...

Issue with code failing to insert object into an array

I've been struggling to add a group of objects from a JSON file into an array. Despite trying to use the push method, the length of the array remains at 0. I'm puzzled by what could be going wrong. The JSON data is being parsed correctly as I&apo ...

AngularJS having trouble navigating to a route with a hash from the login page

I am currently navigating through AngularJS for the first time and I have a navigation bar that allows me to either go to the login page or click on home to return to my homepage. On my homepage, there is a list of products and in the same navigation bar, ...

Creating dynamic JQuery-UI widgets on the flyWould you like to know

One thing I've learned is that by using .on, I can connect functions to DOM elements triggered by certain events. Given my experience with JQuery-ui, I'm interested in generating various JQuery-ui components once they appear on the page. For ins ...

Enhancing the appearance of a Class Component with Material UI using useStyle

I am trying to style the Class Component using useStyle instead of hooks, but I am having trouble figuring out how. Here is my code snippet: import React,{Component} from 'react'; import Avatar from '@material-ui/core/Avatar'; import { ...

How to conceal the day picker in jQuery UI datepicker

I am facing a problem that has been addressed before. As someone with only basic knowledge of CSS, I am a bit confused here. I have two datepickers in my code. <style> .ui-datepicker-calendar { display: none; } </style> Unfortunately, ...

Retrieving Form Data with AJAX and Javascript

When using JavaScript to generate multiple forms and embedding them into HTML using Ajax/JS, the code typically looks like this: function addProduct(product) { var html = ''; html += '<div>&apos ...

Issue encountered while adding platform in Cordova version 8.1.2

Attempting to set up a new Cordova project. The version of Cordova being used is 8.1.2 ([email protected]). I ran the cordova create command and it was successful. However, when trying to add the Android platform with the command cordova platform add ...

How can I add an element to the DOM in React before the render() method is executed?

Currently, I am working with React in conjunction with Rails and incorporating a charting library known as AnyChart. However, the AnyChart code in my render method automatically searches for a <div id="container"></div> element to connect the c ...

Incorporating interactive maps into an AngularJS web application

I've been attempting to integrate Google Maps into my AngularJS application, using the script tag below: <script src="https://maps.googleapis.com/maps/api/js?key=[MySecretKeyHere]&callback=initMap" async defer></script> I found this ...

Bootswatch is causing the Bootstrap tooltip feature to malfunction

Could someone help me figure out why my tooltip isn't functioning properly? The HTML Code in Question: <div class="form-check"> <label class="form-check-label" > <input type="radio" class="form-check-input" name= ...

What is the best way to ensure that an array containing hex colors is accurate and filter out any incorrect values?

I am currently only checking if values are not null or undefined, but I want to ensure that each hex color is correct. If a hex color is incorrect, I need to discard it and move on to the next one. If the format is completely wrong, then I should reset to ...

What is the most secure method for transferring JavaScript variables between websites without using PHP?

The Dilemma: Greetings! Let's say I have ownership of two distinct websites. Site.com and Site.com:3000. There are multiple methods to exchange variables between the two sites. I am unable to utilize localstorage due to being on different domains, ...

Adding or removing a class using Jquery based on the condition of form validation

I am facing a problem with the following code that adds and removes classes to bring the next stage of the form. The form progresses step by step where certain fields need to be filled before validation on the next button, followed by filling other fields. ...