Swapping out the initial hue and toggling between two different colors

Is it possible to keep the color removed after pressing the 1st play SVG, while having two other colors switch back and forth between each other? https://jsfiddle.net/x0pu2c31/

The current colors are Blue. Once one is pressed, I want the colors to switch between Green & Orange.

In the existing code, the colors change between Blue & Green. How can I remove Blue and make the colors switch between Green & Orange once Blue is removed?

Desired behavior:

  • If Blue is pressed, the color should switch back to Green.
  • If Green is pressed, the color should become Orange.
  • If Orange is pressed, the color should go back to Green.
  • Once you click on the play SVG, then close the page by clicking 'X' and return.
.played {
  fill: green;
}

function markAsPlayed(played) {
    played.classList.toggle("played");
}

...

Answer №1

We need to adjust the code so that on the first click, the played state is added but never removed (to avoid going back to blue). Instead, we should check if the played class has been added. If not, add it; if it has, toggle a new class for a different color.

function markAsPlayed(played) {
    played.classList.add("played");
}

function markAsVisited(played) {
    played.classList.toggle("visited");
}

function coverClickHandler(evt) {
    const cover = evt.currentTarget;
    
    hideAll(config.containers);
    resetPage();
    
    if (!cover.classList.contains('played')) {
        markAsPlayed(cover);
    } else {
        markAsVisited(cover);
    }
    showCovers(cover);
}

The CSS now includes extra classes to control color:

.played {
  fill: green;
}

.played.visited {
  fill: orange;
}
// JavaScript functions
const manageUI = (function makeManageUI() {
    // Functions here...
}());

const manageCover = (function makeManageCover() {
    // Functions and configuration details...
}());
manageCover.init({
    container: ".container",
    playButton: ".thePlay"
});

manageUI.init({});
// CSS styles
html,
body {
  // Styles here...
}

.outer {
  // Styles here...
}

// More CSS styles...
<div>
  <div class="outer">
    // HTML content with dynamic classes
  </div>
</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

Develop an AJAX script for processing multiple form inputs in PHP and HTML

I am working on a code that involves multiple form submissions, but the submit functionality is not working due to having multiple submits on one page. I have realized that I need an Ajax script to handle this, but I am new to it. Can someone provide me wi ...

Eliminate the use of ngTemplate in your AngularJS project

I just finished creating an AngularJS project with the yeoman generator, and now it's time to deploy it! After reading up on AngularJS, it appears that the yeoman generator creates Angular projects using templateCache. This means I need to utilize a ...

TineMCE fails to trigger ajax-loading when accessing a textarea

Utilizing $.ajax() to inject this particular piece of HTML into a div on the webpage. <div class="board-container"> <form method="post" action="functions.php"> <input type="hidden" name="function" value="set_boards"> ...

Position the table cells at the corner so they align on the same row as a table row, even if the cells are from a different table with

I am working with two tables, each containing two columns – meaning there are two td elements for every tr. If the last tr of the first table has only one td element, it will result in a blank space at the end. This issue can be resolved by using colspan ...

Can the Angular.js scope be maintained while also making changes to the template?

I am currently facing a challenge with my directive. In the snippet below, I am attempting to extract content from a template, append it to the layout, and then compile it: var $template = angular.element("<div></div>"); $template.append($co ...

The mobile version of the website is currently unable to support video MP4 files

I am facing an issue with displaying an MP4 video file on the mobile version of my website. Oddly, when I inspect the element in the smartphone browser, everything appears to be working fine. However, on the phone itself, the video simply turns off withou ...

Delete the query parameter from the router

In my Vue project, I am using the replace method to add a query to the current URL. If the value is not present, I'm assigning undefined. this.$router.replace({ name: "admin-frs", query: { limit: this.pageSize, page: this.current ...

I am encountering an issue with CreateJS where I receive the error message: "createjs is not defined"

Looking for assistance with my createJS issue. var stage = new createjs.Stage(canvas); Encountering the following error : angular.js:13642 ReferenceError: createjs is not defined, even though I have EaselJS in my bower-components. Appreciate any hel ...

Organize the menu in Angular by sorting it alphabetically

I am currently exploring a way to organize the buttons inside the menu in alphabetical order using a function in the TypeScript file. Please see the code snippet below, which utilizes Angular Material. <mat-menu #menu3="matMenu" [overlapTrig ...

Is there a way to incorporate CSS into a JPG file on the server end?

I have some JPGs that need to undergo CSS operations on the server side, such as cropping and rounding corners. Once processed, I want to serve these edited JPGs to clients. The clients themselves are unable to interpret CSS, they can only display JPGs. I ...

Swapping out a Django template variable for a variable generated using JavaScript

I'm new to Django and Javascript (less than 3 weeks!) and I have a query that may seem naive, so any assistance would be appreciated. In my current code, I have an HTML table rendered with the following snippet: <table border="1" class="dataframe ...

Cut off text inside an HTML anchor element using an ellipsis, while ensuring the image remains at the end - all without the need for JavaScript

How can I truncate link text on a header with an ellipsis but keep the image at the end as a glyphicon? And all of this without using JS. Here's my code: <!DOCTYPE html> <html> <body> <ul> <li> <a href="# ...

The issue with the Bootstrap slider persists

The slider function in the downloaded html and css codes from bootstrap is not working properly. When clicking the arrow, the screen drops below instead of sliding. Even though the code was copied directly from bootstrap, the issue persists. <div c ...

Utilize the HTML img tag in conjunction with AngularJS and Ionic to showcase dynamic images

Currently, I am working on a hybrid mobile app using ionic and Angularjs. I am facing an issue with displaying images through the img html tag in my app. The main layout of my page includes a carousel at the top that displays images and a list containing s ...

The precedence of theme CSS is paramount

Check out this link for Smirnoff Espresso Vodka 70cl (password: hide) Upon inspecting the page source, you'll see that my main stylesheet (main-style) is loaded at the top of the head section with high priority, even above my theme's style. Why ...

How to access the CSS and JS files in the vendor folder using linemanjs

Currently, I am in the process of developing a web application utilizing linemanjs. However, I have encountered an issue where the vendor css and js files are not being referenced correctly when explicitly included. I would appreciate any guidance on what ...

confirming the phone field's character count

My website has multiple phone fields, each formatted like this: $("#HomePhoneInput").inputmask({ "mask": "(999) 999-9999" }); $("#WorkPhoneInput").inputmask({ "mask": "(999) 999-9999" }); $("#CellPhoneInput").inputmask({ "mask": "(999) 999-9999" ...

Combining JSON objects within an array

I'm working with a JSON Array that looks like this: [ {"Name" : "Arrow", "Year" : "2001" }, {"Name" : "Arrow", "Type" : "Action-Drama" }, { "Name" : "GOT", "Type" : "Action-Drama" } ] and I want to convert it to look like this: [ { "Name" : ...

In search of a dropline menu with a comparable style

My search for a menu like the one found on www.ultragraph.co.uk has been exhaustive. I have only come across simple dropline menus, but I am seeking one with hidden sub-menus that reveal upon mouseover similar to ultragraph.co.uk. If anyone has any sugge ...

Easily accessible button and span located on the right side

One issue I'm facing is that the button and span are not aligned on the same line. Is there a way to resolve this? <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/c ...