adjusting the color of ion-button when hovering over the cancel button

I'm working on a button bar for my app and I want the color of the button to change based on its state (false, true).

Currently, the button starts out green, turns light green when hovered over, and becomes white when clicked. Once I click it, the button turns white and the hover effect no longer works (nothing happens when hovered over). I've attempted to change the button's class using the DOM, but the button disappears.

HTML

    <ion-button *ngIf="getAudio() !== undefined" class="musicIcon" id="musicIcon" fill="clear" button (click)="musicToggle()">
      <audio id="backgroundAudio" src="{{getAudio()}}"></audio>
      <ion-icon slot="icon-only" name="musical-notes"></ion-icon>
    </ion-button>

CSS

.musicIcon{
    --color: #047E00;
    --color-hover: #A9FEA6;
}

JS

  musicToggle() {
    this.musicState = !this.musicState;
    const icon = document.getElementById("musicIcon");
    const audio = document.getElementById("backgroundAudio") as any;
    if (this.musicState) {
      audio.muted = true;
      icon.style.color = 'white';
    } else {
      audio.muted = false;
      icon.style.color = '#047E00';
    }
  }

Answer №1

In my opinion, it is advisable to refrain from altering the styles in your javascript logic and instead rely on Ionic for most of the aesthetic aspects. The issue seems to stem from directly setting a static value for the color style, when Ionic could dynamically utilize the values of --color or --color-hover.

Have you familiarized yourself with Ionic's theming? By designating green as the primary color, your <ion-button>s will automatically inherit this color without requiring a custom class.

If you wish for the button to change its color based on the condition this.musicState === true, a straightforward approach would be to specify this directly in the template using the color attribute:

 <ion-button *ngIf="getAudio() !== undefined" [color]="musicState ? 'light' : 'primary'" class="musicIcon" id="musicIcon" fill="clear" button (click)="musicToggle()">
  <audio id="backgroundAudio" src="{{getAudio()}}"></audio>
  <ion-icon slot="icon-only" name="musical-notes"></ion-icon>
</ion-button>

By making this adjustment, you can eliminate your CSS class, and your musicToggle() function will consist of just the following lines:

  musicToggle() {
    this.musicState = !this.musicState;
    const icon = document.getElementById("musicIcon");
    const audio = document.getElementById("backgroundAudio") as any;
  }

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 can I create a customized scrollbar for a div element in an Angular 2.0 CLI project?

I am attempting to create a sleek horizontal scroll bar within one of my div elements, similar to the example shown here: https://i.stack.imgur.com/ziWhi.png My project is based on the angular2 CLI. Progress so far: I came across this package angular2-s ...

What could be causing JSON.parse to encounter errors when parsing a string array?

Within the following function, I am utilizing JSON.parse() on specific string arrays saved in window.sessionStorage. This allows me to apply methods like .map(). window.sessionStorage = { myArray1: "["805746|search|4","980093062|search|0","980113648| ...

The new Date function is malfunctioning on Firefox

Could you please explain why this particular function is not functioning correctly in Firefox (V 34 latest)? It appears to be working successfully on all other browsers, but 'DatePosted' is displaying as Invalid Date. Do you have any insights on ...

Only conceal the table column if space is limited

A scenario involves an HTML table with two columns that need to be displayed fully, with the second column aligned to the right. Currently, this has been achieved by setting the width of the first column to 100%. However, a challenge arises where the words ...

unable to retrieve JSON sub-elements

I encountered an issue while attempting to iterate through the JSON object provided. When trying to access the content-items using page.content-items, I received an error message. Is it possible to access an object that has a key with "-" in its name? Co ...

Adding multiple text as label and sublabel in a React MUI Tooltip can be achieved by utilizing the appropriate

I'm struggling to incorporate a MaterialUI Tooltip into my React application with two separate text lines. The first line should serve as the main title, while the second line functions as a sublabel. In this screenshot provided, you can see where I ...

Error message in Angular js: Unable to load file using XMLHttpRequest

Encountering an error while debugging an Angular JS application in the WebStorm IDE. The error message states: "XMLHttpRequest cannot load file:///C:/Users/../list.html. Cross origin requests are only supported for HTTP." Provided Javascript code : var a ...

Utilizing Google Sheets as a secure, read-only database for Angular applications without the need to make the sheet accessible to the

Seeking a way to utilize Google Sheets document as a read-only database for my Angular application, I have attempted various methods. However, the challenge with all these approaches is that they necessitate public sharing of the Sheet (accessible to anyon ...

Table header containing the weekdays for a jQuery calendar display

I need some assistance in creating a basic monthly calendar using a table. Check out this demo: www.jsfiddle.net/pzdw0s2n/1/ ...

Restriction on Google Maps API: Mouseover functionality is limited to specific hours of the day

Throughout my programming journey, I've encountered some weird errors, but this one takes the cake. I'm stumped as to why it's happening. Here's the situation: My application is supposed to fetch data from a MySQL Database and display ...

Extracting Client's True IP Address using PHP API

Utilizing the following api: I am able to retrieve country, city, and real IP address in localhost (127.0.0.1) successfully. However, when I implement this code on my website, it displays the server's address instead of the client's. How can I r ...

Error message in React Js indicating the absence of a specified file or directory: "Package.json cannot

When I try to run npm start, I encounter the following error - PS D:\React\operations_app_tut> npm start npm ERR! path D:\React\operations_app_tut\package.json npm ERR! code ENOENT npm ERR! errno -4058 npm ERR! syscall open npm ...

Issues with Implementing Scroll Directive in Angular JS

Apologies for asking what may seem like a silly question. I'm still new to using AngularJS and recently came across a neat little scroll directive on http://jsfiddle.net/88TzF/622/. However, when I tried implementing the code in the HTML snippet below ...

Adjusting the X-axis in Highstock: Tips and Tricks

Is there a way to adjust the X axis on this chart? My goal is to shift it so that it only covers half of the width, leaving the other half blank for future plotlines. Any suggestions on how to achieve this? Thanks! :) ...

The listener is not activating the AJAX function

It seems like the function validate() is not being called in the listener, causing an error and preventing data from being added to the database. Here is the code snippet from index.php: <!DOCTYPE html> <html> <head> <title>< ...

Add the file to the current directory

As a newer Angular developer, I am embarking on the task of creating a web page that enables users to upload files, with the intention of storing them in a specific folder within the working directory. The current location of the upload page component is ...

Is there a way to adjust the font size in Javascript/Html without changing the color?

I have a code snippet that creates a button to increment a variable, and I want to change the font size of the displayed variable. This code is for a game akin to cookie clicker. <div class="game-object"> <script type="text/javascript>"; var c ...

Ensure that text within openhtmltopdf is aligned to the right and any unnecessary white space at the

Currently, I am using the open source project openhtmltopdf but I have encountered a certain issue. Within a div-element, I have a p-tag with text-align: right styling. The text is aligned correctly to the right until there is a line break due to the widt ...

Please display the "django.db.models.query_utils.DeferredAttribute object at" message in Django

Having trouble retrieving data from a database object to populate and edit a form in HTML. When I try to access the current value from the attribute, I encounter this issue: Form HTML <form enctype="multipart/form-data" method="pos ...

Node.js bypasses unit test validation

As a beginner in BDD with Node.js, I have a controller function defined as follows: var getUser = function(username, done) { console.log('prints'); User.findOne({ 'local.username': username }, function (err, user) { ...