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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    May 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy Hiding cells with a certain innerText

    I have a table which I would like to hide certain cells depending on when their value is a certain value.

    I have the below code as shown below.


    Code:
    var table = document.getElementById('myTable');
    cells = table.getElementsByTagName('td');
    
    for (var len=cells.length, i=0 ; i<len; i++)
    {
    if(cells[i].innerText = "New Client")
    {
    cells[i].style.display = 'none';
    }
    }
    When I use this function, instead of only hiding the cells with the values "New Client", instead it is hiding nearly all other cells, only leaving a few cells showing.

    does anyone know why this could be?

    Thanks

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,092
    Thanks
    23
    Thanked 594 Times in 593 Posts
    This
    Code:
    if(cells[i].innerText = "New Client")
    assigns "New Client" to every cell.
    Use the == testing equality.
    Use innerHTML instead of innerText and if you use cells[i].style.display = 'none'; you will take the cell out of the table, cells[i].style.innerHTML = ''; will leave a blank cell in it's place
    Code:
    <html>
    <head>
    </head>
    
    <body>
    	<table width="90" id="myTable" cellpadding="2" cellspacing="2" border="1">
    		<tr style="height:40px;"><td></td><td>New Client</td></tr>
    		<tr style="height:40px;"><td></td><td></td></tr>
    		<tr style="height:40px;"><td></td><td></td></tr>
    	</table>
    
    
    <script type="text/javascript">
    var table = document.getElementById('myTable');
    cells = table.getElementsByTagName('td');
    
    for (i=0 ; i<cells.length; i++)
    {
    	if(cells[i].innerHTML == "New Client")
    	{
    		cells[i].innerHTML = '';
    	}
    }
    </script>
    </body>
    </html>

  • Users who have thanked sunfighter for this post:

    mrloz123 (05-20-2013)

  • #3
    New to the CF scene
    Join Date
    May 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Smile Thanks

    Ahh thank you so much!!

    This has been bugging me for AGES! I actually do want to remove the column from the table so I have kept cells[i].style.display = 'none', changing it to if(cells[i].innerHTML == "New Client") worked a treat and now my report works perfectly.

    Thanks again is much appreciated


  •  

    Tags for this Thread

    Posting Permissions

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