How to Hide a HTML TABLE which dont have any ID in Javascript

Discussion in 'JavaScript' started by neil12345, Nov 29, 2010.

  1. #1
    I have following HTML Table, what i want to do is i want to hide this table in a page using Javascript, problem is that i dont have control on the HTML ( the table is displaying dynamically from a CMS in a page ), so i want to keep it hidden using Javascript, is that possible without the table having id ?

     
    neil12345, Nov 29, 2010 IP
  2. Cash Nebula

    Cash Nebula Peon

    Messages:
    1,197
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Whatever is used to insert the table, surround it with a div that you can hide.
    
    <html>
    <head>
    <script type="text/javascript">	
       function hideTable() {
          document.getElementById('table').style.visibility = 'hidden';
       }
    </script>
    </head>
    <body>
    <div id='table'>
       <table>
          <tr><td>Table</td></tr>
       </table>
    </div>
    <button onclick="hideTable()">Hide Table</button>
    </body>
    </html>
    
    Code (markup):
     
    Cash Nebula, Nov 29, 2010 IP
  3. neil12345

    neil12345 Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thats the problem i cannot add anything to it because that Table is coming dynamically which i dont have any control. I mean its nested table inside table etc...
     
    neil12345, Nov 29, 2010 IP
  4. Cash Nebula

    Cash Nebula Peon

    Messages:
    1,197
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yeah, not having an ID is a problem but you can hide all tables on the page with this:
    
    <html>
    <head>
    <script type="text/javascript">	
       function hideTable() {
          var tables = document.getElementsByTagName('table');
          for (i=0; i<tables.length; i++) {
             tables[i].style.visibility = 'hidden';
          }
       }
    </script>
    </head>
    <body>
    <table>
       <tr><td>Table 1</td></tr>
    </table>
    <table>
       <tr><td>Table 2</td></tr>
    </table>
    <button onclick="hideTable()">Hide Tables</button>
    </body>
    </html>
    
    Code (markup):
     
    Cash Nebula, Nov 30, 2010 IP
  5. mephisto73

    mephisto73 Greenhorn

    Messages:
    53
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #5
    You have to be able to select it uniqely in the DOM. Is it always the first table on the page (or second or third...?)
    If so you can select the first table in the DOM and hide it with out the need of any id's.
     
    mephisto73, Dec 1, 2010 IP