Using jQuery conditionals to dynamically alter the CSS background properties

Currently, I am in the process of creating HTML thermometers that are animated using jQuery to visually represent a percentage number. However, despite my efforts, I have not been successful in changing the color of the thermometer based on the level it represents. In the script provided in the Fiddle I've created, all thermometers end up being blue regardless of the value they indicate.

The jQuery code I am using is as follows:

$('.lvl').each(function(){
    var level = $(this).text();
    $(this).animate( {
        width: level+'%'
    }, 1000);

    var lvlwidth = $(this).width();
    if (lvlwidth < 80) {
        $(this).css('background-color', 'blue');
    } else if (lvlwidth > 80) {
        $(this).css('background-color', 'yellow');
    }
});

Update: My goal is for the color of the bar to change dynamically as it grows. For example, I want it to be blue when it's under 40, then switch to yellow, and finally turn red when it reaches 80.

Answer №1

UPDATE:

http://jsfiddle.net/rpxr4bjj/4/

$('.lvl').each(function () {
    var progress = $(this).text();
    $(this).animate({
        width: progress + '%',
    }, {
        step: function () {


            if (progress < 80) {
                $(this).css('background-color', 'blue');
            } else if (progress > 80) {
                $(this).css('background-color', 'yellow');
            }
        }
    }, 3000);


});

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 retrieve values from similar class/properties using Selenium in C#?

Currently, I am in the process of automating tests to compare expected and actual endorsements on a summary page. I am facing a challenge in reading all the endorsements values displayed on the summary page. The number of endorsements can vary from 2 to 5 ...

Handling overflow and z-index of tabs in Vuetify Drawer

Struggling to create an expandable sidebar menu using Vuetify2, following the documentation, but unable to prevent the expanded menu from overlapping other elements on the page. The current behavior causes items to be pushed away and wrap onto the next ro ...

Utilizing Selenium with Python to choose a specific option from a dropdown selection

I am currently experimenting with using Selenium in Python to choose the option "Custom date" from the dropdown menu displayed in the image below: https://i.sstatic.net/ASHU2.png The hierarchy of divs is structured as shown here: https://i.sstatic.net/xtA ...

What could be causing the CSS modifications I made to not show up on my website?

I've been working on putting together a portfolio website as part of my web design course. I've set up a style sheet for the site, but I'm having some trouble seeing the changes reflected online. When I check the site locally in Chrome, all ...

Obtaining the height of a div's parent element while utilizing the d-flex class in Bootstrap

I'm having trouble getting the navigation menu to be the full height of the bordered div and I can't seem to pinpoint where the problem lies. <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_em ...

Distribute the text evenly on either side of the center

I have a unique layout with two datalist elements centered, text positioned above, and two buttons integrated. body { background-color: DarkGray; } h1 { color: black; font-size: 30px; font-family: 'lucida console' } span { color: #4434 ...

Step-by-step guide on generating and showcasing an event exclusively using the Calendar Strip React Native npm module

I recently started exploring the world of React Native and Expo, and I have been diving into the documentation for Calendars in React Native and Expo. During my research, I came across a fantastic npm package called Calendar Strip that caught my attention. ...

Modify the CSS styles (which were set by a preceding function) upon resizing the screen using Javascript

In the mobile version of a website (width less than 620px), there is a button in the header. Clicking this button triggers the "mobileNav" function, which toggles the visibility of the mobile navigation between "display: none;" and "display: flex;". This f ...

Data retrieval is currently not functioning, as React is not displaying any error messages

One of the components in my app is as follows: import React, { Component } from 'react'; import PropTypes from 'prop-types'; import { connect } from 'react-redux'; import { compose } from 'redux'; import { translate ...

Increase the gap between columns in Bootstrap for a better layout

I have implemented a web layout using bootstrap 3. You can view the JSFiddle here. In order to make the layout responsive, I had to structure it in a slightly unconventional way. The final output on small screens includes three information boxes within an ...

Does this extensive combination of pseudo classes hold true?

Here's the code snippet I am working with: .fontmenu .fontlist{ position: absolute; bottom:30px; display:none; } .fontbutton button:hover .fontmenu .fontlist{ display:block; } <div class="fontmenu"> ...

What are the advantages of using GET over POST in AJAX implementation?

I'm curious as to why the GET method is still being utilized in AJAX requests. Take this example: $.ajax({ type: "GET", url: "SomeController/GetSomething", data: { id: 100} }); GET is useful for storing data in URLs, such as when querying Goog ...

The div height adjustment peculiarities in IE7 and IE8 are causing quite a stir

I recently encountered a problem with my HTML/JS code that I thought was simple. The code is designed to expand the size of a div on mouseover and then collapse it back on mouseout. Here's how the code looks: CSS: .sign-in-up { position: absolut ...

Encountering 'Unacceptable' error message when attempting to retrieve response via AJAX in the SPRING

I'm encountering an issue with my code where I am trying to retrieve a JSON array response from a controller class. Whenever I send a request from JavaScript, I receive a "Not Acceptable" error. Can someone please assist me in identifying the bug in m ...

Childnode value getting replaced in firebase

Every time I attempt to push values to the child node, they keep getting overridden. How can I solve this issue and successfully add a new value to the child node without being overwritten? ...

Invoke JavaScript function upon page load

There is a link on my page that, when clicked, opens a login popup. However, I am struggling to make it work on page load. I have spent a lot of time trying to figure this out, but nothing seems to be working. <a id="onestepcheckout-login-link" href=" ...

observing calls made with twilio using javascript

My goal is to track the status of a phone call happening between two people using their phone numbers. Let's say +558512344321 and +558543211234 are currently on a call. As soon as the call begins, I want the browser to display "Call in progress," and ...

Enhancing web page interactivity through dynamic element names with Javascript and jQuery

I have an interesting HTML setup that looks like this: <div> <input type="text" name="array[a][b][0][foo]" /> <input type="text" name="array[a][b][0][bar]" /> <select name="array[0][a][b][baz]>...</select> </div> ...

The javascript file jquery-ui-1.10.4.custom.js is encountering an issue with the error message 'this.source is not a function'

I am encountering an error while using 'jquery-ui-1.10.4.custom.js' for auto-complete. The error being thrown is 'TypeError: this.source is not a function' Here is the HTML code snippet: <input type="text" ng-model="srchfname" auto ...

What is the best way to create a button that triggers a dice reroll while keeping the same form data through Ajax?

Currently, I am utilizing Flask for my application and expanding my knowledge of Javascript. My project involves creating a dice app where users can input values (like the number of dice) and then roll the dice. I am aiming to implement a feature that allo ...