Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove "empty" paragraphs with jQuery

Tags:

jquery

My writers have a bad habit of creating empty paragraphs. (I.e., paragraphs that have nothing inside them, like:

<p></p>

Is there a jQuery function that removes "empty" paragraphs. Here's what I've tried:

$('p').remove(":contains(' ')"); // nope! bad logic, all p's have empty spaces 

$("p:empty").remove() // nope, doesn't work, doesn't remove any p's

Anything else I should try?

like image 941
a53-416 Avatar asked Oct 19 '11 15:10

a53-416


Video Answer


1 Answers

$("p:empty").remove() works for me (demo at http://jsfiddle.net/AndyE/XwABG/1/). Make sure your elements are actually empty and don't contain any white space.

You can always use filter():

// Trimming white space
$('p').filter(function () { return $.trim(this.innerHTML) == "" }).remove();

// Without trimming white space
$('p').filter(function () { return this.innerHTML == "" }).remove();

Working demo: http://jsfiddle.net/AndyE/XwABG/

like image 82
Andy E Avatar answered Sep 28 '22 06:09

Andy E