0

I'm trying to use $.trim() to trim whitespace in empty elements so I can check the length of an element to see if it has text content in it and if it does not then remove it from the DOM.

Some elements are returning a length of greater than 0 because they have spaces and/or carriage returns in them which is all that is contained in them.

I basically need to find and remove any empty tags when they have nothing in them.

I don't suppose anyone knows of a simple quick way to just check for and remove any empty tags in the main content of a page, whether they contain any whitespace chars or not by any chance? I'm sure there must be a way.

1
  • I don't exactly get the question: If you use trim() on the tag's content and it returns an empty string, then no content is in the tag and it can be removed. Commented Dec 20, 2011 at 14:19

4 Answers 4

5

You'd want to check for elements in the body only, since otherwise script tags and other non-content elements also get removed: http://jsfiddle.net/6zdNf/1/.

// filter empty elements after trimming
$("body *").filter(function() {
    return $.trim($(this).html()) === "";
}).remove(); // remove them
Sign up to request clarification or add additional context in comments.

9 Comments

Nice. I like your version :).
Your version seems like it should be more efficient than mine, since you're removing all the empty nodes at once instead of piecemeal. However, why do you test for .html instead of .text?
@mblase75: I was not sure if an element containing another empty element is considered empty - as per the question it would not be because it does not consist merely of spaces/newlines. By filtering using .html(), such elements are not removed.
@pimvdb But if the innermost element is empty, then once you remove it, its parent element is also empty -- but if jQuery doesn't loop through nodes from the inside-out, it won't detect these and you'll be left with lots of empty parent nodes. Using .text avoids this.
This seems like it would work as I want it to, and yeah, the elements that are empty but contain children which are also empty, should be removed as well since there is styling for things that may cause the page to work weird when they're just empty but not collapsable. I don't know how efficient this would be however, since it would bubble up the DOM each time, while searching for * but i'll give it a go. Thx
|
1

Simply use jQuery filter with a function:

var rege = /\S/gi;
$('div').filter(function(){
  return rege.test($(this).text())
});

After using this code, you'll only have divs left with text in it! Use with caution on images, since they do not have any text!

The regular expression /\S/gi test for a non-space character. Empty tags won't match and will return false.

Comments

1
$('body *').each(function() {
    if (!$.trim($(this).text())) {
        $(this).remove();
    };
});

Don't expect it to run very efficiently, though.

3 Comments

This is basically what I did but it wasn't working to remove carriage returns. It would still have a length for the elem.
Really? According to the jQuery docs it should remove newlines. Unless you're considering <br/> tags to be removable?
I think that's actually part of the issue, but I can't remember if I did see a br or not now. I just know that it wasn't removing the space effectively enough because I was still getting a length > 0 back after trimming like that.
1

Try this:

$('body *').each(function(idx, element) {
    if ($(element).children().size() == 0) {
        if ($(element).text().trim() == "") {
            $(element).remove();
        }
    }
});

You may need to further fine-tune the selector a bit, but it should do it.

2 Comments

You forgot to save your fiddle.
Ouch! I'll remove the link then. Thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.