Ask your JavaScript questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

color code the <div>'s based on rating, without a lot of errors. JavaScript

  • SOLVED

I have a for loop and I want to color code the <div>'s based on rating, without a lot of errors. I want to use jquery. Is there a more concise way to do this? The errors break the jquery ui sortable.

$bloke = $("#I" + (i)); colorize($bloke);

<script>
function colorize() {
if ($skill >= 2400) { $($bloke).css("background-color", "#4FAF7F"); return;}
if ($skill >= 2200) { $($bloke).css("background-color", "#9370DB"); return;}
if ($skill >= 2000) { $($bloke).css("background-color", "#A5B275"); return;}
}
</script>

Answers (1)

2018-12-12

Rainner Lins answers:

Would be helpful if you could provide more info about the errors you're getting, but from looking at your code I see that your colorize() function needs access to two things, $bloke and $skill. Depending on how your code is setup your function may not have access to that, so try passing them in like this:


function colorize( $bloke, $skill ) {
$skill = $skill | 0; // make sure it's a numeric value for comparing
if ( $skill >= 2400 ) { $bloke.css("background-color", "#4FAF7F"); return; }
if ( $skill >= 2200 ) { $bloke.css("background-color", "#9370DB"); return; }
if ( $skill >= 2000 ) { $bloke.css("background-color", "#A5B275"); return; }
}


The calling it like this:


// ...
colorize( $bloke, $skill );
// ...