2

I have a table that I'm trying to color based on the values in the table. So, if the value in the table is 150%, the cell would be colored red. If the value in the table is 50%, the value would be colored green. However, for some reason, the actual text in the table has a multitude of spaces within them as well as a % symbol. How can I replace any spaces and '%' characters with nothing ('')?

Here is what I have that isn't working:

<script type="text/javascript">

        $(document).ready(function () {
            $('#myTable td.PercentMem').each(function () {
                if ($(this).text().replace(/\s/g, '').replace('%', '') >= '100') {
                    $(this).css('background-color', '#ff0000');
                }
                else {
                    $(this).css('background-color', '#33cc33');
                }
            });
        });

    </script>

Thank you!

EDIT

I'm dumb. I forgot to include the fact that these percentages have a decimal point in them. So, what I'm really dealing with is something like "50.89%", and the solutions provided thus far convert that into "5089". How can I keep the decimal?

2
  • Would be easier to either search for all numbers or replace all non-numbers instead. Commented Dec 16, 2020 at 19:46
  • Additionally, you've to convert the resulting string to number, lexicographic comparison gives unexpected results with certain numbers, ex. "9" > "100" == true. Commented Dec 16, 2020 at 19:46

2 Answers 2

2

You need to strip out all non-numeric charactes plus convert the resulating string to an int. So something like this: parseInt($(this).text().replace(/\D/g,'')). Also, in your post you're comparing to a string '100', which obviously should be a number. Try this:

        $(document).ready(function () {
            $('#myTable td.PercentMem').each(function () {
                if (parseInt($(this).text().replace(/\D/g,'')) >= 100) {
                    $(this).css('background-color', '#ff0000');
                }
                else {
                    $(this).css('background-color', '#33cc33');
                }
            });
        });

Disclaimer: I have not actually tested this, but this should work.

Sign up to request clarification or add additional context in comments.

3 Comments

That still wouldn't work because you are doing >= '100' which is a string not a number.
Thanks for the help! I tried to implement this solution, but no matter what the values are the cell turns out red for me. Would you be able to explain what the "/\D/g" is?
@Pecker Sure thing. \d and \D are regex metacharacters. Here are their definitions: \d (digit) matches any single digit (same as [0-9]). The uppercase counterpart \D (non-digit) matches any single character that is not a digit (same as [^0-9]). \g simple means do it for all occurrences (globally). So replace(/\D/g,'') replaces all non-digits with an empty string.
1

You can achieve with this, avoid to use jQuery to select elements, use pure JavaScript:

const myCells = document.querySelectorAll('.PercentMem');

myCells.forEach(cell => {
  const cellValue = cell.textContent.replace(/\D+/g, '');
        
  cell.classList.toggle(parseInt(cellValue) >= 100 ? 'red' : 'green');
});

Here is the complete fiddler with all the code.

7 Comments

Thank you for the help! I tried out this solution, but no success :(. The cells didn't change to red or green with this case. I would try to troubleshoot your solution for my case, but I don't understand any of the syntax in your code
Sadly, have you checked the fiddler link? There is a complete solution as you need.
I have checked out the link. It works great there! Would you be able to explain what the "/\D+/g" does?
Yeah sure, it is a Regex condition to remove any other char than numbers.
Oh my... I'm sorry, I forgot to include one major thing! The percent's I'm dealing with look more like "50.89%", so in removing all non-numeric characters, I'm making that into 5089. How can I remove the spaces and percent but not the decimal?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.