I am trying to hide the following element in an automatically generated HTML document:
If the desire is to mimic the functionality of the :empty
selector except that whitespace is ignored, the accepted answer (by scunliffe) doesn't quite work. It only checks for child elements, and this doesn't account for text directly inside the selected element. For instance, <p>Hello World!</p>
would be treated as empty because it has no child elements even though it does contain non-whitespace text.
My solution uses the jQuery.trim() function to remove leading and trailing whitespace from the .text() value which contains the combined text contents of the selected element and its descendants. So the selected element is hidden if it contains no non-whitespace text and no child elements. As with the :empty selector, HTML comments are not counted as content since they are not reflected in either the .text() or .children() values.
$('p.sitspagedesc').each(function(){
if($.trim($(this).text()) == '' && $(this).children().length == 0){
$(this).hide();
}
});
See the Fiddle at https://jsfiddle.net/TNTitan89/crejkbxq/.
Here is my solution which I just implemented for a client using jQuery 1.5.x - you might have to adjust the //skip empty tags but which are valid
regular expression string.
$('*:only-child:empty').each(
function(index) {
var currentElement = $(this);
// skip singleton tags
if(/^(?:area|br|col|embed|hr|img|input|link|meta|param)$/i.test(currentElement.get(0).tagName) == true) {
return
}
// skip empty tags but which are valid
if(/^(?:textarea)$/i.test(currentElement.get(0).tagName) == true) {
return
}
while (currentElement.parent().children().length == 1) {
currentElement = currentElement.parent();
}
// so 0 or more children for the parent then we hide it
// we will never have more then 0 children though the :empty takes care of that
console.log('hidding: ' + currentElement);
currentElement.hide()
}
);
While not a standard, Firefox has ":-moz-only-whitespace".
Also, for some "future proofing", css-tricks mentions a :blank
selector that will be part of the CSS Selectors Level 4 draft. While no current browser supports it, it is a possibility.
The :empty
selector is indeed very strict. An element containing a space is not considered empty. So there are two solutions
Maybe you can combine both. The :empty
selector is a CSS3 selector and is not yet supported by IE8 and before, so a Javascript fallback might be a good idea for those browsers, unless you can fix the server side scripting so that the empty elements are not rendered at all, or are given your special class during rendering, so no Javascript is needed.
css:
.sitspagedesc:empty
{
display:none;
}
jquery:
$('.sitspagedesc').html(function(){
// empty element
return $.trim($(this).html());
});
I don't think you can do it with pure CSS.
However with a little JavaScript you can do it.
var allParas = document.getElementsByTagName('p');
//filter by class name if desired...
for(var i=0;i<allParas.length;i++){
if(allParas[i].getElementsByTagName('*').length == 0){
allParas[i].style.display = 'none';
}
}
If you have access to jQuery it is a little easier to do the filtering with their built in selectors.
$('p.sitspagedesc').each(function(){
if($(this).children().length == 0){
$(this).hide();
}
});