0

I have a table that is styled based on cell content. Cell contents are limited to specific data so I created an array. I can style the cell if I reference an index of the array:

var greencell = ["item1", "item2", etc]
var cells = document.getElementById("mytable").getElementsByTagName("td");
for (var i = 0; i < cells.length; i++) {
  if (cells[i].innerHTML == greencell[0]) {
    cells[i].style.backgroundColor = "#80ff80";
  }
}

However, I need to access the entire array. When I set comparison == greencell the styling is not applied. What am I missing? Am I even approaching this correctly? Loop through the array similar to the cells loop and set comparison on the loop result? My mind is mush at the moment

Thanks!

0

3 Answers 3

1

You need to loop through the array:

var greencell = ["item1", "item2", etc]

var cells = document.getElementById("mytable").getElementsByTagName("td");
var j = 0; 
for (var i = 0; i < cells.length; i++) {
    for (j = 0; j < greencell.length; j++) {
        if (cells[i].innerHTML == greencell[j]) {
            cells[i].style.backgroundColor = "#80ff80";
        }
    }
}

If you want a different color for each item, you can use an array:

var colors = ["#ff0000", "#00ff00", "#0000ff"];
var cells = document.getElementById("mytable").getElementsByTagName("td");
var j = 0; // You shouldn't redeclare the variable each iteration
for (var i = 0; i < cells.length; i++) {
    for (j = 0; j < greencell.length; j++) {
        if (cells[i].innerHTML == greencell[j]) {
            cells[i].style.backgroundColor = colors[j];
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

“Redeclare the variable each iteration” doesn’t make sense. var is a parse-time thing and an iteration is a run-time thing.
Thanks @balint I was going that direction but didn't get the syntax correct.
@Ryan Right, sorry, I'm still thinking in other languages.
1

You can use indexOf to check if the innerHTML matches any element of greencell:

if (greencell.indexOf(cells[i].innerHTML) !== -1) {
    cells[i].style.backgroundColor = "#80ff80";
}

1 Comment

Thanks Ryan, this is nice! I find this to be the most challenging aspect of coding, so many ways to reach a solution.
0

For a more ES6 oriented solution you could iterate over the HTMLCollection as an Array and then decide your style base on the text of the cell.

HTML:

<table id="mytable">
  <tr>
    <td>item2</td>
    <td>wrong</td>
  </tr>
  <tr>
    <td>red</td>
    <td>item1</td>
  </tr>
</table>

JS:

var greencell = ["item1", "item2"];
var redcell = ["red", "wrong"];

var cells = document.getElementById("mytable").getElementsByTagName("td");

Array.from(cells).forEach(function(cell) {
  cell.style.cssText = getStyles(cell.innerHTML)
})

function getStyles(value) {
  if (greencell.indexOf(value) > -1) {
    return 'background-color: green; color: white';
  }
  if (redcell.indexOf(value) > -1) {
    return 'background-color: red; color: white';
  }
}

JSFiddle

Comments

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.