Ionic fails to change icon color when loading existing data from server

Apologies for the vague subject, finding it difficult to accurately describe this issue.

I have implemented a rating system for specific content. The rating page is displayed in a modal. Initially, when the user rates the content for the first time, everything functions as expected. However, if the user does not refresh the data from the server and revisits the same review/modal to edit their review - including changing the rating which alters the colors of the stars (from 1 to 5 stars).

The issue arises when the user refreshes the data or opens the app for the first time, fetching data from the server. The modal displays all the correct ratings and reviews from their previous entry. But, if they attempt to change the star rating they previously gave (e.g., changing from a 2 to a 4), although the data recorded from the function shows the new value as 4, visually nothing changes - it still only displays two colored stars.

I have included console messages to track the iteration through the star/rating color function. Despite the console messages showing the correct colors being assigned, the actual color of the stars remains unchanged.

Relevant CSS:

.reviewColor {
  color: lightgray ;
}

The modal structure:

<ion-modal-view class="modalReviews">
  <ion-header-bar>
    <div class="headerBar">
      <div class="headerBarDiv">PRODUCT REVIEW</div>
      <button type="button" class="button button-block button-small-custom headerBarButton" ng-click="closeReviews()"> X </button>
    </div>    
  </ion-header-bar>

  <ion-content>
  <div id="reviewWrapper">
    <div ng-if="doReview == 1" id="doReview" class="prodSection prodReviewCard">
      <div ng-if="prod.reviews.userReviewDate != 0">
        Last review submitted on: {{prod.reviews.userReviewDate}}
      </div>
      <div class="" style="text-align:center;">
        <i class="ion-ios-star reviewColor ratingSize" id="Rating_{{prod.ceID}}_1" ng-click="reviewRating('Rating',prod.ceID,1);" ng-style="1 <= prod.reviews.userRating && {'color' : eColors.orange}"></i>
        <i class="ion-ios-star reviewColor ratingSize" id="Rating_{{prod.ceID}}_2" ng-click="reviewRating('Rating',prod.ceID,2);" ng-style="2 <= prod.reviews.userRating && {'color' : eColors.orange}"></i>
        <i class="ion-ios-star reviewColor ratingSize" id="Rating_{{prod.ceID}}_3" ng-click="reviewRating('Rating',prod.ceID,3);" ng-style="3 <= prod.reviews.userRating && {'color' : eColors.orange}"></i>
        <i class="ion-ios-star reviewColor ratingSize" id="Rating_{{prod.ceID}}_4" ng-click="reviewRating('Rating',prod.ceID,4);" ng-style="4 <= prod.reviews.userRating && {'color' : eColors.orange}"></i>
        <i class="ion-ios-star reviewColor ratingSize" id="Rating_{{prod.ceID}}_5" ng-click="reviewRating('Rating',prod.ceID,5);" ng-style="5 <= prod.reviews.userRating && {'color' : eColors.orange}"></i>
      </div>
      <div style="margin-top:10px;">
        <textarea style="border:1px solid lightgray;width:100%;height:100px;" maxlength="255" ng-model="userReview" id="userReview">{{prod.reviews.userReview}}</textarea>
      </div>
      <div style="margin-top:20px;">
        <input type="checkbox" id="userAnon" ng-model="userAnon" ng-true-value="'{{userDisplayName}}'" ng-false-value="'Anonymous'" style="margin-left:25px;margin-right:5px;vertical-align:bottom;width:20px;height:20px;" ng-checked="prod.reviews.userAnon != 'Anonymous'"></input>Display '{{userDisplayName}}' with review? 
      </div>

      <div style="margin-top:20px;">
        <button type="button" class="button button-block button-large-custom" ng-click="reviewSubmit();">
          Submit Review
        </button>
      </div>
      <div style="margin-top:15px;color:{{reviewColor}};">{{reviewMsg}}</div>
    </div>
  </div>
</ion-content>

The controller function responsible for dynamically changing star colors:

  $scope.reviewRating = function(idName,ID,rating) {
    for (var i=1;i<=5;i++) {
      var tName = idName+ "_" +ID+ "_" +i ;
      console.log(tName) ;
      var star = document.getElementById(tName) ;
      if (i <= rating) {
        console.log("Orange") ;
        star.style.color = eColors.orange + " !important" ;      
      } else {
        console.log("Gray") ;
        star.style.color = "lightgray !important" ;
      }
    }
    $scope.userRating = rating ;
    console.log("new rating: " +$scope.userRating) ;
  }

The console messages indicate that the function is executing correctly and assigning the new rating to $scope.userRating. However, visually, the star colors remain unchanged - displaying only two orange stars:

Rating_1135_1 = Orange    controllers.js:2705 
Rating_1135_2 = Orange    controllers.js:2705 
Rating_1135_3 = Orange    controllers.js:2705 
Rating_1135_4 = Orange    controllers.js:2705 
Rating_1135_5 = Gray      controllers.js:2708 
new rating: 4             controllers.js:2712

This issue occurs only when a previous review/data is loaded from the server - it works fine when no existing data is present, even allowing users to save their reviews, exit the modal, and return later to make modifications. It seems to be specifically related to refreshing or fetching new data from the server when an existing review is already stored.

Answer №1

One issue arises when trying to include !important while setting a style property (

star.style.color = 'lightgray !important';
).

Instead, it is recommended to assign a class and give the class style declaration higher specificity to override .reviewColor without using !important.

An alternative approach is to eliminate the inline !important since it is considered poor practice, and apply the style without it (which should suffice unless an alternate style rule contains !important as inline styles take precedence).

If there's a genuine need to incorporate !important in the inline style, you can utilize element.style.setProperty and specify the priority in the third parameter:

star.style.setProperty('color', 'lightgray', 'important');

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 could be causing a successful return from JQuery Ajax but still showing an error in Firebug within a PHP application?

I'm encountering an issue with my form submission and jQuery validator plugin. Everything seems to be working fine until I reach the submit handler in my jQuery code: submitHandler: function (form) { $.ajax({ type: "POST", url: ...

Converting nested arrays to objects via JSON transformation

I am faced with a nested JSON array structure like this: Parent: { Child1: [ {name:'grandchild1', value:'abc', checked:true}, {name:'grandchild2', value:'pqr', checked:false} ], Ch ...

Adding Space Before Bullet Points in HTML: A Quick Guide

Is there a way to add space before bullet points? My requirements are as follows: This is paragraph 1 The first text line The second text line This is paragraph 2 The first text line The second text line I would greatly appreciate any ...

There was an error in AngularJS ng-idle where it could not read the property 'watch' because it was undefined

Encountering an issue with the idle timeout in the login control, I am facing the error "cannot read property 'watch' of undefined." In an attempt to execute the code on Tomcat Server 7 and Angular 1.5 $scope.started = false; function closeMod ...

My Bootstrap navbar seems to be malfunctioning. I'm wondering if there could be an issue with

here is a snippet of the code I'm working on: <nav class="navbar navbar-expand-md navbar-dark bg-dark sticky-top"> <div class="container-fluid"> <a class="navbar-brand" href="#"> <img src="https://i.stack.imgur.com/5XeYV.png" ...

Exploring Protractor functionality by testing ui-bootstrap-alert with the dismiss-on-timeout attribute

When running my protractor test, I encountered an issue where an alert is displayed when an error occurs in the app. Here is a snippet of the relevant HTML: <uib-alert type="danger" dismiss-on-timeout="5000" close="closeAlert()"> <span>Err ...

Issue with ThreeJs raycaster not returning correct event.clientY value when header bar is visible

Here's the issue: I'm trying to place a sphere on a 3D model when I click on it using ThreeJs raycaster. It works fine when the canvas covers the entire page, but if I add a header bar to the page, the positioning is off. Here are some visual ex ...

Expanding the dimensions of an SVG without utilizing CSS

My SVG file contains a polyline element with specific points. <?xml version="1.0" encoding="utf-8"?> <svg width="280" height="280" viewBox="0 0 140 140" stroke="blue" fill="none"> <polyline points="21,14 18,14 15,7 10 ...

Having trouble with Firefox not recognizing the linear gradient color in my CSS3 code

I'm working on implementing a linear gradient background using CSS, but I'm facing an issue with Firefox not displaying it correctly. body { height: 100%; /*background-color: #F0F0F0;*/ background-repeat: repeat-x; /* Safari 4-5, Chrome 1-9 */ ...

Strategies for sending data in the body of a GET request in a React.js API

I'm having an issue passing data in the body of a GET type API request in my React.js app. Here is the code I am using, but the API isn't receiving any data. getUnits = ( key, text, code, limit, offset ) => { let data = JSON.st ...

Choosing various choices using AngularJS

My goal seems simple using vanilla JS, but with AngularJS, I'm looking for the best way to achieve it within the framework. I aim to update the selected options in a multiple select box without adding or removing any options. Below is a snippet of my ...

What is the method for adding data to a table without utilizing an ID field?

Here is the code I have written: This table contains details of the candidates: <table id="candidates-table" class="table table-striped table-bordered"> <thead class="thead-dark"> <tr> <th scope="col">Candidate Picture ...

Issue with dynamically passing select values to pop-up link in Firefox

My website has a select dropdown feature that triggers a confirmation pop up with the same selection when the user makes a choice. Upon clicking Submit, a new window opens with the corresponding link. Everything works smoothly in Safari, Chrome, and Opera ...

How to retrieve HTML attribute using D3 techniques

Looking to iterate through all rect nodes in the code snippet below: d3.selectAll("svg g rect") .on('mouseover', function (d) { console.log(this); }); When Console.log is executed, the following is printed: <rect class="cls" na ...

What is the best way to refresh my view model while using chained promises?

I've recently started learning about promises and I'm facing a challenge with updating an object in my view from two chained promises: function Test($resource, FC, UserDetailsService) { 'ngInject'; var self = this; se ...

Utilize Google Home to browse through nearby websites and respond to inquiries by incorporating Javascript

Can Google Home be programmed to read from a local webpage and provide answers based on the content of that page through Javascript? ...

When attempting to reload a single page application that utilizes AJAX, a 404 error is encountered

Recently, I've been working on coding my personal website and have successfully created a single page application using ajax. The content is dynamically loaded between the header and footer whenever a navigation bar link is clicked. To enable the back ...

What is the best way to utilize ajax for submitting and fetching content?

It's pretty obvious that I'm a complete novice when it comes to JavaScript and jQuery, but I have a query regarding ajax requests. Here's what I'm trying to achieve but struggling with: I've defined a content.append('<di ...

Can you explain the use of the "left" and "right" fields in the jstree jQuery plugin?

Currently, I am working on creating a tree using the jquery plugin jstree. While going through this plugin's database file, I came across two fields called "left" and "right". However, I am unclear about how these fields are utilized. Interestingly, ...

Leveraging useEffect (or a comparable method) within a class component to create a loading screen

As a React newbie, I recently created a loading screen using useEffect in a functional component. Now, I'm trying to achieve the same using class components, but I'm facing some challenges. Here is the functional component that works perfectly: c ...