boulder606
boulder606

Reputation: 29

Hide cell with jquery when empty ( no content)

I am trying to hide empty cells with jquery but the code doesn't work

<script>
jQuery(document).ready(function() {
    if(jQuery('.tabcontent_01_custom_property_fields').html())
    {
      ( $("th:empty").text().length == 0) .css('display', 'none');
    }
});

Any tips would be great.

Upvotes: 2

Views: 254

Answers (1)

mxlse
mxlse

Reputation: 2784

You already have the :empty selector, which is enough. See the working fiddle:

HTML:

<body>
  <table class="table">
    <tr>
      <td>Test</td>
      <td></td>
      <td>Test 2</td>
      <td></td>
      <td>Test 3</td>
    </tr>
  </table>
</body>

JS:

jQuery(document).ready(function() {
  if(jQuery('.table').html())
  {
    ( $("td:empty").css('display', 'none'));
  }
});

https://jsfiddle.net/8vym5vk8/1/

You could even shorten the JS:

jQuery(document).ready(function() {
     ( $(".table td:empty").css('display', 'none'));
});

https://jsfiddle.net/8vym5vk8/2/

Upvotes: 2

Related Questions