I'm confused as to why the icon color isn't changing on the Blogger homepage for each individual user

I'm experimenting with changing the eye color based on visitor count. It works perfectly fine on static posts in my Blogger, but it fails to update the eye color properly on my homepage according to the set numeric values. Instead of displaying different colors based on the value, it only shows one color regardless of the number, and it changes every time I click on "load more articles" at the bottom of the homepage.

What could be causing this issue? I'm not very proficient in coding; I simply found code online and combined it by following tutorials.

My website is .

Here's the code snippet I'm working on:

Please pay attention to the // coloring eye section.

I'm looking for an easy solution to fix this problem. I need help synchronizing the eye icon color on both the blogger homepage elements and static post elements based on their respective view counts.


Here are the approaches I have tried along with images.

1st Attempt:

I used this code:

// coloring eye
let eye = document.getElementById("matamata");

if (count < 300) {eye.style.color = "grey";} 
else if (count >= 300 && count < 600) {eye.style.color = "yellow";}
else if (count >= 600 && count < 1000) {eye.style.color = "orange";} 
else {eye.style.color = "red";}

Result from this attempt on homepage and post views:

Homepage view: https://i.stack.imgur.com/7IDek.jpg

Post view

https://i.stack.imgur.com/EZSBy.jpg

2nd Attempt:

I used this code:

// coloring eye
$.fn.colorize = function () {
   return this.each(function() {
      var $this = $(this);
      $this.css({color: count < 200 ? "grey"
                      : count < 500 ? "yellow"
                      : "red"});
   });
};
$("i.far.fa-eye").colorize();

The result of this attempt:

Homepage view: https://i.stack.imgur.com/Y4E9S.jpg

Post view: https://i.stack.imgur.com/j1FOJ.jpg

I prefer the second attempt, but it displays the whole code instead of individual article eye colors on the homepage view. Both codes work well when viewed from the post perspective.

Answer №1

  • Unique identifiers are essential
  • Function should be defined outside the loop
  • The function should accept a count parameter

Below is an updated version without using FN

Make sure to execute this code AFTER the icons have been displayed, specifically after the

$.each($("a[name]"), function (i, e) { ............ });
, and not within it.

$("i.far.fa-eye").each(function() {
  const count = +$(this).closest(".entry-meta").find(".viewsini").text().replace(/\D/,"");
//  console.log(count)
  $(this).css({color: count < 200 ? "grey" : count < 500 ? "yellow" : "red" });
})
.fa-eye:before {
    content: "👁";
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="entry-meta">
  <span class="entry-author">
<span class="by">By:</span><span class="author-name">52UL</span></span>
  <span class="entry-time"><time class="published" datetime="2020-07-29T20:42:00+08:00">29.7.20</time></span><span class="postviews1" style="margin-left:5px; "><a name="4567839109293122938"></a><i class="far fa-eye" id="matamata" style="color: grey;"></i><span id="post-4567839109293122938"><span class="viewsini" id="postviews">1,849</span></span>
  Views <span class="menarik">MENARIK</span><span class="hangat">HANGAT</span></span>
</div>
<div class="entry-meta">
  <span class="entry-author">
<span class="by">By:</span><span class="author-name">mhdazln</span></span>
  <span class="entry-time"><time class="published" datetime="2020-07-29T20:38:00+08:00">29.7.20</time></span><span class="postviews1" style="margin-left:5px; "><a name="5430741819652172389"></a><i class="far fa-eye" id="matamata" style="color: grey;"></i><span id="post-5430741819652172389"><span class="viewsini" id="postviews">207</span></span>
  Views <span class="menarik">MENARIK</span><span class="hangat">HANGAT</span></span>

</div>

<hr />

<h1>Blog post</h1>
<div class="entry-meta">
<div class="align-left">
<span class="entry-author"><span class="author-avatar-wrap"><span class="author-avatar lazy-ify" data-image="//3.bp.blogspot.com/-pI9uAjf9vsY/XvDqs0wJtkI/AAAAAAAACQc/8QRyrYzMrNUP5lisRPNuHIy2WcBlKowAQCK4BGAYYCw/w72-h72-p-k-no-nu/123.jpg" style="background-image:url(https://3.bp.blogspot.com/-pI9uAjf9vsY/XvDqs0wJtkI/AAAAAAAACQc/8QRyrYzMrNUP5lisRPNuHIy2WcBlKowAQCK4BGAYYCw/w29-h29-p-k-no-nu/123.jpg)"></span></span>
<span class="by"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">By:</font></font></span><span class="author-name"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">52 UL</font></font></span></span>
<span class="entry-time"><time class="published" datetime="2020-07-29T20:42:00+08:00"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">29.7.20</font></font></time></span><span class="postviews1" style="margin-left:5px; "><a name="4567839109293122938"></a><i class="far fa-eye" id="matamata" style="color: grey;"></i><span id="post-4567839109293122938"><span class="viewsini" id="postviews"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">858</font></font></span></span><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">Views</font></font><span class="menarik"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">ATTRACT</font></font></span><span class="hangat"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">WARM</font></font></span></span>


</div>
<div class="align-right">
</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

What is the best way to ensure that third tier menus open in line with their respective items?

Check out this fiddle link: http://jsfiddle.net/Wss5A/72/ Focusing on the following: 2-A-1 2-A-2 2-A-3 The third-tier menu is currently displayed starting from the top left corner. I want the first submenu element to be aligned with either 2-A-1, 2-A- ...

Utilize the identical function value within a Vue template

In my template, I am encountering a recurring situation where I need to retrieve a value from a function. This function takes parameters from the template (such as the index of a v-for loop) and returns a result that is then displayed on the template. The ...

How can we efficiently add a new node to a polyline link in gojs while maintaining the original positions of points in the links adjacent to the inserted node

After posting a question on StackOverflow (Seeking Javascript library for displaying and editing networks of nodes and edges), I was directed to the gojs splice sample. The sample has been helpful, but I've hit a roadblock trying to achieve the speci ...

What is the best way to transform a GET request with a query string into a promise?

After successfully making a request with querystring params, I encountered an issue while trying to promisify my request: // Works var Promise = require("bluebird"); var request = Promise.promisifyAll(require("request")); request.get({ url: 'htt ...

stop the leakage of CSS and JS from the subtree to the document through the inverse shadow DOM mechanism

My page contains dynamic HTML content that I want to incorporate. The dynamic content consists of only HTML and CSS, without any JavaScript. However, I have some custom global CSS styles and JS logic that need to be implemented along with this dynamic con ...

Determining the Clicked Button in ReactJS

I need help with a simple coding requirement that involves detecting which button is clicked. Below is the code snippet: import React, { useState } from 'react' const App = () => { const data = [ ['Hotel 1A', ['A']], ...

Struggling to make npm and sqlite3 function properly on MacOS Catalina

Struggling with npm package installation in a project directory on my Mac has proven to be quite the challenge. Each attempt at a simple npm install results in perplexing errors that I can't seem to grasp. The hurdle seems to center around specific p ...

Is it possible to install a Chrome extension specifically for YouTube on Google Chrome?

Hey, I'm trying to eliminate thumbnail images from YouTube. The code I am currently using is: while (true) { $("ytd-thumbnail").remove() } As of now, when I input this code in the console, it successfully removes all thumbnail images. However, I ...

Integrate ThreeJs models into an Angular JS application

I have a question that pertains to my webapp development. I am currently utilizing Angular Js (v1.5/1.6) and I would like to incorporate some minimalistic 3D animated models by integrating Three Js. Although I have attempted to configure certain aspects, ...

What steps can be taken to enhance the responsiveness of this Bootstrap 4 code?

I've been trying to make this code more responsive, but I haven't found a suitable solution yet. When the browser window shrinks to mobile sizes, the list becomes too narrow and the picture/video items on the right end up small and aesthetically ...

What is the most effective method for creating posts, pages, and their corresponding URLs using Next.js and MongoDB

Here's a glimpse of my MongoDB data: { "_id": { "$oid": "630f3c32c1a580642a9ff4a0" }, "title": "this is a title", "paragraph": "this is a paragraph" } Now, let's t ...

Errors encountered by the Chrome extension

Hey there, I've recently delved into creating a chrome extension but hit a roadblock. My issue revolves around the service worker registration failing and encountering errors related to undefined properties. https://i.stack.imgur.com/bGzB4.png The c ...

Unable to reinitialize MUI DatePicker after keydown event

Encountering an unusual behavior that defies explanation has left me puzzled. You can explore the code sandbox here. I am attempting to enable resetting a readOnly field by pressing DEL or BACKSPACE, but it seems to be ineffective. Oddly enough, I can suc ...

Extracting server error messages on the client side using Node.js

Before storing data in the database, my server performs validation checks. If it is unable to save the data into the database, an error message is sent. In my client-side application, how can I retrieve and display this error message? Below is the HTTP r ...

Angular list with a repeating group of radio buttons

I have a set of 'options', which consists of the following: {Id: 1, Label: "option 1"}, {Id: 2, Label: "option 2"} Additionally, I have a list of 'products' structured as follows: {Id: 1, Name: "Name 1", Recommend: options[0]}, {Id: ...

Tips for displaying a React component with ReactDOM Render

_Header (cshtml) <div id="Help"></div> export default class Help { ReactDOM.render( <Help/>, document.getElementById('Help') ); } Help.js (component) } My objective is to di ...

At times, the map may only be visible in the top left corner of its designated container

Currently, I am integrating the Google Maps API v3 for JavaScript into my project. However, I am facing an issue where the map only appears in the upper left corner under certain circumstances. To visualize this problem, you can visit this link and click ...

Leveraging the power of jQuery to capture and recycle a dynamically generated date

Using a jQuery plugin, a list of dates is generated. Implementing moment.js works perfectly fine as shown in this fiddle (http://jsfiddle.net/UJ9z4/). However, when attempting to apply it to the live file with the plugin running, an undefined error is enc ...

React useState Error: Exceeded maximum re-renders. React enforces a limit on the number of renders to avoid getting stuck in an endless loop

Can someone help me troubleshoot the 'Too many re-renders' error I'm encountering? I've implemented the try, catch method along with React hooks like useState and setState. My goal is to fetch data from an API and display it on a ...

Selecting elements by their name using vanilla JavaScript

I am facing an issue where I have to assign a value to a checkbox based on certain variables. The challenge lies in the naming convention used in the HTML I am working with: <input id="jc_1" type="checkbox" name="jc[1]"> <input id="jc_2" type="c ...