Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help with Cell value changing row color

    Hello All,

    I'm fairly new to javascript and I need some help with my written logic. I have a simple datatable being sourced from an Oracle DB. There is a column whereas each cell contains only one character - "R","Y", or "N".

    If I use the "alert(cell[14].textContent); or alert(cell[14].innerText);" I get the returned values in the browser. So I know my logic is focused on the right column/cells.

    The issue is when I try to apply a color change on the row background color based on the cell value of either R,Y or N nothing happens...? Here is my code so far, any help would be much appreciated.

    Code:
      
            var rows = document.getElementById("trans_separate").getElementsByTagName("tbody")[0].getElementsByTagName("tr");
            //alert(rows.length);
    
            for (i = 0; i < rows.length; i++) {
                cells = rows[i].getElementsByTagName('td');
    			
              if (cells[14].textContent === 'R') rows[i].className = "red";
              else if (cells[14].textContent === 'Y') rows[i].className = "yellow";
              else if (cells[14].textContent === 'N') rows[i].className = "green";  
            }
    Thank you

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,105
    Thanks
    23
    Thanked 594 Times in 593 Posts
    example with 5 rows and 5 tds:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    </head>
    
    <body>
    <table border="1" id="trans_separate">
    	<tr>
    		<td>R</td>
    		<td>2</td>
    		<td>3</td>
    		<td>4</td>
    		<td>5</td>
    	</tr>
    	<tr>
    		<td>1</td>
    		<td>2</td>
    		<td>3</td>
    		<td>4</td>
    		<td>5</td>
    	</tr>
    	<tr>
    		<td>Y</td>
    		<td>2</td>
    		<td>3</td>
    		<td>4</td>
    		<td>5</td>
    	</tr>
    	<tr>
    		<td>1</td>
    		<td>2</td>
    		<td>3</td>
    		<td>4</td>
    		<td>5</td>
    	</tr>
    	<tr>
    		<td>N</td>
    		<td>2</td>
    		<td>3</td>
    		<td>4</td>
    		<td>5</td>
    	</tr>
    </table>
    
    <script type="text/javascript">
    var rows = document.getElementById("trans_separate").getElementsByTagName("tr");
    for (i = 0; i < rows.length; i++) {
    	cells = rows[i].getElementsByTagName('td')[0].innerHTML;
    	if (cells == "R") rows[i].style.backgroundColor = "red";
    	if (cells == "Y") rows[i].style.backgroundColor = "yellow";
    	if (cells == "N") rows[i].style.backgroundColor = "green";
    }
    </script>
    </body>
    </html>

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,216
    Thanks
    75
    Thanked 4,344 Times in 4,310 Posts
    Not to ask a silly question, seeing as how I don't see any significant difference between Sunfighter's code and crh313's, is it possible that crh313 forgot to actually *CREATE* classes in his <style> section by those names? And/or that the classes are declared in such a way that they can't apply to <tr> elements?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,216
    Thanks
    75
    Thanked 4,344 Times in 4,310 Posts
    Anyway, there is no need to use getElementsByTagName for either <tr>s or <td>s. There are builtin properties that are easier to use and faster.
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <style type="text/css">
    tr.red { 
        background-color: red;
        color: yellow;
    }
    tr.yellow {
        background-color: yellow;
        color: blue;
    }
    tr.green {
        background-color: green;
        color: yellow;
    }
    </style>
    </head>
    <body>
    <div>
    <table border="1" id="trans_separate">
    <tr><td>R</td><td>2</td><td>3</td><td>4</td><td>5</td></tr>
    <tr><td>G</td><td>2</td><td>3</td><td>4</td><td>5</td></tr>
    <tr><td>R</td><td>2</td><td>3</td><td>4</td><td>5</td></tr>
    <tr><td>Y</td><td>2</td><td>3</td><td>4</td><td>5</td></tr>
    <tr><td>N</td><td>2</td><td>3</td><td>4</td><td>5</td></tr>
    </table>
    </div>
    
    <script type="text/javascript">
    (
      function( )
      {
          var colors = { "R":"red", "Y":"yellow", "G":"green" };
          var rows = document.getElementById("trans_separate").rows;
          for (r = 0; r < rows.length; ++r )
          {
               var row = rows[r];
               var cname = colors[row.cells[0].innerHTML];
               if ( cname != null ) row.className = cname;
          }
      }
    )();
    </script>
    </body>
    </html>
    Last edited by Old Pedant; 03-21-2013 at 12:53 AM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •