${IDHERE} element's text color not changing when tab is switched

One dilemma I encountered involves displaying a percentage on a webpage with 2 tabs. The percentage is originally shown on the tab that is open when the page loads.

The JavaScript code I used is quite straightforward:

  adaPercentColor(percent, ada) {
    if (ada === 0) {
      document.getElementById('ada').style.color = 'red';
    } else if (percent <= 1 && percent >= .95) {
      document.getElementById('ada').style.color = 'green';
    } else if (percent <= .94 && percent >= .85) {
      document.getElementById('ada').style.color = 'yellow';
    } else {
      document.getElementById('ada').style.color = 'red';
    }
  }

Additionally, here is the Angular HTML snippet:

              <ion-card-title id="ada">{{rows.ada}}</ion-card-title>

Initially, this setup works perfectly fine. However, I faced an issue where the color resets to black when switching between tabs. To address this, I invoke the JS function within a fetchData() method that is triggered in ngOnInit().

Answer №1

When working within the angular framework, it is advisable to avoid using pure JavaScript. Instead, consider utilizing a code snippet like

<ion-card-title id="ada" [style.color]="myColor">{{rows.ada}}</ion-card-title>
. In this setup, myColor functions as a public variable in your component which can be updated dynamically:

adaPercentColor() {
  if (ada === 0) {
    this.myColor = 'red';
  } ... and so forth
}

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

Tips for showing data from an hour ago in Angular

Here is the code snippet provided: data = [ { 'name' : 'sample' 'date' : '2020-02-18 13:50:01' }, { 'name' : 'sample' 'date' : '2020-02- ...

Troubleshooting Flexbox Sticky Footer: Issue with 'Flex: 1' not properly filling the height

Currently experimenting with the flexbox sticky footer technique to create a dynamic-height footer, which has been a challenge for me. However, I'm facing an issue where the main section does not expand to fill the entire height of the window even aft ...

Having issues with your PHP dropdown database code?

I am having trouble with the code below. It seems to display the HTML portion correctly, but it is not fetching the data from my database. My goal is to populate a drop-down list with data from the database and then display it in a table below when the u ...

Removing all text inside an input field with Vue

I am trying to create a password input field using type 'text' instead of 'password.' <input type="text" v-model="form.password" @input="test" /> <input type="hidden" v-model="form.hiddenPassword" /> As part of my approach ...

Determine if the input text field contains any text and store it in a variable using jQuery

I'm working on a form that includes radiobuttons and textfields. To keep track of the number of checked radiobuttons, I use this code: var $answeredRadiobuttons = $questions.find("input:radio:checked").length; But how do I store the number of textf ...

Solving the Problem of Input Values with Jquery and Javascript

I am facing a challenge in making a div vanish with the class 'backarea' while simultaneously displaying another div with the class 'successLog' on the screen. The catch here is that I want this transition to occur only when specific us ...

Changing the size of the logo as you scroll through the page

I have implemented the following code to resize the logo as I scroll down the page. $(document).on('scroll', function() { if ($(document).scrollTop() >= 10) { $('.logo img').css('width', '50px'); } else ...

Scaling down in CSS does not center the element properly using the margin 0 auto technique when the scaled element was larger than the container before scaling

Upon scaling down an element that previously overflowed its container, I noticed that margin: 0 auto no longer centers the element as expected. Interestingly, even using transform-origin: center center did not fix this issue. It seems that the auto margins ...

Transferring sound file/blob from user interface to Servlet for storage on server

I am struggling to successfully send our audio file from the UI using the provided code snippet: var url = (window.URL || window.webkitURL).createObjectURL(blob); var link = document.getElementById("save"); link.href = url; link.download = filename || &ap ...

Loading screen featuring an outlined blueprint of design elements

Can anyone identify the technology or language used to create the preloader screen on websites like the one shown in this link? The elements have a sweeping shine effect while loading. This style of preloader screen can be seen on popular websites such as ...

How can you dynamically disable a radio option button using Angular rendering without relying on an ID?

Is there a way to disable the male radio button without using an id, and utilizing angular rendering2? It seems like it's not working for me. I need to make this change only in the form.ts file, without altering the HTML code. form.html <label& ...

What is the reason that the CSS3 property name 'filter' is not recognized in Windows 8 applications?

I'm in the process of adapting my own codepen for a Windows 8 desktop app. Everything runs smoothly on Codepen, with the exception of some CSS adjustments from -webkit- to -ms-. However, I encountered an issue regarding this specific line in the CSS s ...

Invoking a Jquery function through a GridView link

After attempting various methods for hours, I am throwing in the towel. It's possible that someone here might spot what I'm missing. I have a GridView containing a Delete button. Upon clicking on the ClientClick event, a javascript function is in ...

What is the proper way to eliminate the port from a URL so that the images sourced from the XAMPP database can function correctly?

I am a beginner in Angular and I am facing an issue with Angular and XAMPP. I am attempting to load images from mySQL database where I stored their destinations. The problem is that Angular is trying to access that destination through this link: https://i ...

What is the best way to incorporate several functions within a resize function?

Is it possible to incorporate multiple functions within the windows.width resize function? I have been experimenting with some code, but I would like to restrict its usage to tablet and mobile devices only, excluding laptops and PCs. Any suggestions on h ...

The webpack production build consistently displays the message "This site is running on the development build of React."

I have been attempting to utilize webpack-4 for generating a production build of my React project (not built with Create React App), but I am facing difficulties. The project is written in TypeScript and uses ts-loader, as well as React version 15.6.2. Be ...

Using the prop callback in a React test renderer does not trigger updates in hooks

I am currently exploring ways to effectively test a React function component that utilizes hooks for state management. The issue I am encountering revolves around the onChange prop function not properly updating the state value of my useState hook. This in ...

Timezones not synchronizing properly with Moment.js

Hello everyone, I am currently exploring the world of moment.js along with the timezone add-on. I'm encountering some issues with a world clock project that involves using moment.js and moment-timezone.js together on a page where highcharts are also p ...

D3.json request falls flat

Is failure inevitable in this scenario? d3.json("https://api.kraken.com/0/public/Time", function(){ console.log(arguments); }); I'm encountering a familiar CORS hurdle XMLHttpRequest cannot load https://api.kraken.com/0/public/Time/. No ...

"Unraveling nested data structures in React using JSON parsing

When attempting to fetch data in React from a MySQL database, I have encountered an issue where MySQL auto-escapes my values, including nested objects. While I am able to use the .json() function successfully on the top-level, I have run into problems when ...