6

I have the following HTML :

<div id="rightCon">




                    </div>

And then I have the following script at the top :

$('#rightCon:empty').hide();

Why is the div not hiding? I can see that there is some spaces(that I can´t get ridoff) but does that really matter? How do I remove/hide this div when its empty with only spaces?

Lucio
  • 4,083
  • 3
  • 42
  • 72
Banshee
  • 15,436
  • 37
  • 114
  • 205
  • Possible duplicate of [Hiding an element that contains only spaces using CSS](https://stackoverflow.com/questions/13380906/hiding-an-element-that-contains-only-spaces-using-css) – Eugen Konkov Apr 11 '18 at 09:59

3 Answers3

7

Your element appears to have a bunch of white space, which will give it a text node. It won't be matched by :empty for that reason.

You could try finding the element and checking it's contents explicitly:

$('#rightCon').filter(function() {
  var text = $(this).text().replace(/\s*/g, '');
  return !text;
}).hide();
Pointy
  • 389,373
  • 58
  • 564
  • 602
4

This solved the problem.

$(document).ready(function () {
                if($('#rightCon').text().trim().length < 1)
                {$('#rightCon').hide();}
            });
Banshee
  • 15,436
  • 37
  • 114
  • 205
1

Your div is not actually empty (It contains whitespace). So the selector $("#rightCon:empty") will never evaluate and hide the div.

Since HTML elements should be unique, you can safely assume that you can select the correct element via:

var rightCon = $("#rightCon");

You can then hide the element via:

right.hide();

Or

 $("#rightCon").hide();
Darren
  • 66,506
  • 23
  • 132
  • 141