Set CSS rules depending the value of a table cell

Piero Alberto

How can I apply my CSS rules on a <td> with a certain value inside?

    <table>
      <tr>
        <td>Name</td>
        <td>John</td>
      <tr>
    </table>

For example, how can I set the color:black in the td with the value John? Is it possible only with CSS? How?

David Wilkinson

Here is a JS solution, which loops through all instances of td's and checks their contents. It appends a class to td's which have inner content of "John" in my example.

var el = document.getElementsByTagName("td");
for (var i = 0; i < el.length; i++) {
  if(el[i].innerHTML == "John") {
    el[i].className += " " + "test";
  }
}
.test {
  color: red;
}
<table>
  <tr>
    <td>Name</td>
    <td>John</td>
  <tr>
</table>

EDIT - the above is a basic example of course - you can expand and tailor to your needs.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Set an automatic color background depending on the HEX value in the cell?

From Dev

How to set DataGrid cell content depending on binding value?

From Dev

How to set DataGrid cell content depending on binding value?

From Dev

How to set the background colour of the rows in a spreadsheet depending on the value of a cell in the row

From Dev

Cell colours depending on value

From Dev

Change table cell color depending on value in asp and C#

From Dev

Change cell font colour in a table depending on value - RoR

From Dev

Conditional SELECT depending on a set of rules

From Dev

Set Value via Custom Table Cell

From Dev

Google Apps Script - set formula of active cell depending on value of another cell

From Dev

jQuery set css value for a div depending on the position of another div

From Dev

Fillup cell depending on value of another cell

From Dev

Firebase: set security rules depending on user roles

From Dev

How to set the rules depending on the entity field?

From Dev

CSS table column rules

From Dev

EXCEL- Need to split a table to individual sheets depending on cell value without any blanks

From Dev

Change background color of HTML table cell depending on drop-down list value?

From Dev

How to assign cell value depending on row and column header in vue js table

From Dev

Change the formatting of a cell depending on the value of that cell and another cell

From Dev

Is it possible to set an Excel Pivot Table filter value to a cell reference?

From Dev

Set table cell value based on index using jQuery

From Dev

Find a cell value and fill other cells depending on the value of the cell

From Dev

Add a class to a cell depending on data value

From Dev

Copying and Pasting Row and Column Depending on cell value

From Dev

VBA Selecting Columns depending on a cell value

From Dev

SSRS Change cell color depending on SUM value

From Dev

conditional formatting rules for a cell value - absolute value

From Dev

Set cell value with VBA

From Dev

Set a string to a value in a cell?

Related Related

  1. 1

    Set an automatic color background depending on the HEX value in the cell?

  2. 2

    How to set DataGrid cell content depending on binding value?

  3. 3

    How to set DataGrid cell content depending on binding value?

  4. 4

    How to set the background colour of the rows in a spreadsheet depending on the value of a cell in the row

  5. 5

    Cell colours depending on value

  6. 6

    Change table cell color depending on value in asp and C#

  7. 7

    Change cell font colour in a table depending on value - RoR

  8. 8

    Conditional SELECT depending on a set of rules

  9. 9

    Set Value via Custom Table Cell

  10. 10

    Google Apps Script - set formula of active cell depending on value of another cell

  11. 11

    jQuery set css value for a div depending on the position of another div

  12. 12

    Fillup cell depending on value of another cell

  13. 13

    Firebase: set security rules depending on user roles

  14. 14

    How to set the rules depending on the entity field?

  15. 15

    CSS table column rules

  16. 16

    EXCEL- Need to split a table to individual sheets depending on cell value without any blanks

  17. 17

    Change background color of HTML table cell depending on drop-down list value?

  18. 18

    How to assign cell value depending on row and column header in vue js table

  19. 19

    Change the formatting of a cell depending on the value of that cell and another cell

  20. 20

    Is it possible to set an Excel Pivot Table filter value to a cell reference?

  21. 21

    Set table cell value based on index using jQuery

  22. 22

    Find a cell value and fill other cells depending on the value of the cell

  23. 23

    Add a class to a cell depending on data value

  24. 24

    Copying and Pasting Row and Column Depending on cell value

  25. 25

    VBA Selecting Columns depending on a cell value

  26. 26

    SSRS Change cell color depending on SUM value

  27. 27

    conditional formatting rules for a cell value - absolute value

  28. 28

    Set cell value with VBA

  29. 29

    Set a string to a value in a cell?

HotTag

Archive