JavaScript Change DIV Background Color, Depending on Its Width change background color in photoshop,change background color google docs,

I'm wanting to use jQuery to dynamically modify the background color of a <div>, based on its CSS property, width value.

The usage is for some form of color-coded meter, which indicates how well (or poorly) a device performs in a specific category (there are 5), and then there is one 'overall' category, which produces the overall score, based on a little math (add all 5 together and divide the answer by 5).

I have tried two methods, one based on the little jQuery knowledge I have, and the other adapted from an answer on SO. Both are included in the JSFiddle I have provided, with the latter commented out.

Here are the colors and the ranges of the widths for each:

  • 0-24% = red - #a41818
  • 25-49% = orange - #87581c
  • 50-74% = yellow - #997815
  • 75-90% = yellowgreen - #7ba01c
  • 91-100% = green - #3a8d24

Thanks!

Answer:1

I'd suggest:

$('.rating-bar').css('background-color', function(){
    var percentage = parseInt($(this).data('size'), 10);
    if (percentage > 0 && percentage < 25){
        return '#a41818'
    }
    else if (percentage > 24 && percentage < 50) {
        return '#87581c';
    }
    else if (percentage > 49 && percentage < 75) {
        return '#997815';
    }
    else if (percentage > 74 && percentage < 90) {
        return '#7ba01c';
    }
    else if (percentage > 89 && percentage <= 100) {
        return '#3a8d24';
    }    
});

JS Fiddle demo.

Answer:2

There were a few semantic problems ($(e) used instead of $(this), ($(document).ready nested strangely), and the logic you've used requires the ratio of each bar's width to the parent bar, not the width itself.

Try this: http://jsfiddle.net/VFSUN/1/

$(document).ready(function () {
    var bar = $('.rating-bar');
    bar.css("background-color", "#7ba01c");

    var parentWidth = parseInt($('.rating-bar-bg').css("width"));
    $('.rating-bar').each(function () {
        var e = $(this).css("width");
        e = parseInt(e);
        ratio = e / parentWidth * 100;
        if (ratio <= 24) {
            $(this).css("background-color", "#a41818");
        } else if (ratio >= 25 && e < 50) {
            $(this).css("background-color", "#87581c");
        } else if (ratio >= 50 && e < 75) {
            $(this).css("background-color", "#997815");
        } else if (e >= 75 && e < 91) {
            $(this).css("background-color", "#7ba01c");
        } else if (ratio >= 91) {
            $(this).css("background-color", "#3a8d24");
        }
    });
});
Answer:3

I need to get the window inner height of the window, taking into account the x-scroll bar. but it seems window.innerHeight returns the same height regardless of whether or not the x-scroll bar is ...

I need to get the window inner height of the window, taking into account the x-scroll bar. but it seems window.innerHeight returns the same height regardless of whether or not the x-scroll bar is ...

  1. window innerheight vs outerheight
  2. window innerheight css
  3. window innerheight mobile
  4. window innerheight iframe
  5. window innerheight jquery
  6. window innerheight javascript
  7. window.innerheight
  8. window.innerheight vs document.documentelement.clientheight
  9. window.innerheight vs screen.height
  10. window.innerheight not working
  11. window.innerheight vs document.body.clientheight
  12. window.innerheight js
  13. window.innerheight react
  14. window.innerheight safari mobile
  15. window.innerheight undefined
  16. window.innerheight document.documentelement.clientheight
  17. window.innerheight scrollbar
  18. window.innerheight browser support
  19. window.innerheight vs clientheight
  20. window.innerheight too big

I'm working on a REST interface to a library system that uses the SIP2 protocol (https://en.wikipedia.org/wiki/Standard_Interchange_Protocol) and was able to get things working on a system that doesn'...

I'm working on a REST interface to a library system that uses the SIP2 protocol (https://en.wikipedia.org/wiki/Standard_Interchange_Protocol) and was able to get things working on a system that doesn'...

I want to access the value of variable a inside var2() function. However, it gives an error saying the value is not defined. <html> <head> <script type="text/javascript"> function ...

I want to access the value of variable a inside var2() function. However, it gives an error saying the value is not defined. <html> <head> <script type="text/javascript"> function ...

  1. access variable from another class java
  2. access variable from another class c#
  3. access variable from another function python
  4. access variable from another class python
  5. access variable from another method java
  6. access variable from another file python
  7. access variable from another class swift
  8. access variable from parent class python
  9. access variable from main method java
  10. access variable from class python
  11. access variable from inner class java
  12. access variable from parent class java
  13. access variable from another js file
  14. access variable from header file c++
  15. access variable from another method c#
  16. access variable from another php file
  17. access variable from function python
  18. access variable from outside function javascript
  19. access variable from another script unity
  20. access variable from another activity android

I want to make an angular body-scroll only table. To make it simpler, I created two tables, one of which only has the headers, and the other table is for the body. The body table is wrapped into a div ...

I want to make an angular body-scroll only table. To make it simpler, I created two tables, one of which only has the headers, and the other table is for the body. The body table is wrapped into a div ...

  1. angularfire callback