Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 954507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:13:35+00:00 2026-05-16T00:13:35+00:00

I coded a php page that displays information from a mysql database neatly into

  • 0

I coded a php page that displays information from a mysql database neatly into tables. I would like to hide empty table rows with an onLoad event handler.

Here is a sample table with code that hides a <td> when it has no content. but i can only get it to work with different IDs:

        <script type="text/javascript">
        function hideTd(id){
            if(document.getElementById(id).textContent == ''){
              document.getElementById(id).style.display = 'none';
            }
          }
        </script>
        </head>
        <body onload="hideTd('1');hideTd('2');hideTd('3');">
        <table border="1">
          <tr>
            <td id="1">not empty</td>
          </tr>
          <tr>
            <td id="2"></td>
          </tr>
          <tr>
            <td id="3"></td>
          </tr>
        </table>
    </body>

what i want to do is use a class for the <td>s to achieve the same thing while only referencing the class once, and not referencing every single id that I want to remove, which will not even work for my dynamic content. I tried using this code:

    <script type="text/javascript">
    function hideTd(){
        if(document.getElementsByClassName().textContent == ''){
          document.getElementsByClassName().style.display = 'none';
        }
      }
    </script>
    </head>
    <body onload="hideTd('1');">
    <table border="1">
      <tr>
        <td class="1">not empty</td>
      </tr>
      <tr>
        <td class="1"></td>
      </tr>
      <tr>
        <td class="1"></td>
      </tr>
    </table>
</body>

but it does not work. its supposed to hide the empty <td>s that have the specified class. how do i hide empty <td>s using classes, not IDs?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T00:13:36+00:00Added an answer on May 16, 2026 at 12:13 am

    There are several issues:

    1. Class names (and IDs) are not allowed to start with a digit.
    2. You have to pass a class to getElementsByClassName().
    3. You have to iterate of the result set.

    Example (untested):

    <script type="text/javascript">
    function hideTd(className){
        var elements = document.getElementsByClassName(className);
        for(var i = 0, length = elements.length; i < length; i++) {
           if( elements[i].textContent == ''){
              elements[i].style.display = 'none';
           } 
        }
    
      }
    </script>
    </head>
    <body onload="hideTd('td');">
    <table border="1">
      <tr>
        <td class="td">not empty</td>
      </tr>
      <tr>
        <td class="td"></td>
      </tr>
      <tr>
        <td class="td"></td>
      </tr>
    </table>
    </body>
    

    Note that getElementsByClassName() is not available up to and including IE8.

    Update:

    Alternatively you can give the table an ID and use:

    var elements = document.getElementById('tableID').getElementsByTagName('td');
    

    to get all td elements.

    To hide the parent row, use the parentNode property of the element:

    elements[i].parentNode.style.display = "none";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

PHP/MySQL newbie here. I managed to develop a web page that displays info from
I have a table created with PHP from an Sql Database that successfully displays
I have my php coded page on one server that returns a json result.
I have a PHP script that works by calling items from a database based
I have a simple page in a PHP/MySQL web application that lets admin users
I am trying to make a page that displays articles in a database but
I am developing an application the returns data from a MYSQL database and displays
I have a web page coded in PHP. This page is for posting a
i have this simple code to load data from other php page using get
I have a one page site that has PHP code in it. Once the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.