Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide HTML element containing only spaces using jQuery?

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?

like image 923
Banshee Avatar asked Sep 06 '13 23:09

Banshee


People also ask

How can you hide an element but still keep its space in document?

visibility:hidden hides the element, but it still takes up space in the layout. display:none removes the element from the document. It does not take up any space.

Can we use a jQuery method to hide an HTML element?

By using show( ) and hide( ) methods you can show and hide HTML elements in jQuery.

How do I hide a div but keep the space?

The visibility: hidden rule, on the other hand, hides an element, but the element will still take up space on the web page. If you want to hide an element but keep that element's space on the web page, using the visibility: hidden; rule is best.

How can you hide a div element along with the space it takes using CSS?

CSS Demo: visibility To both hide an element and remove it from the document layout, set the display property to none instead of using visibility .


1 Answers

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();
like image 79
Pointy Avatar answered Oct 22 '22 04:10

Pointy