How do I remove empty p tags with jQuery?

The answer depends on what “empty” means. If the empty paragraphs are <p></p> then fireeyedboy’s p:empty selector is the way to go. If there could be spaces or newlines or other such things then you’ll probably want something like this:

$('p').each(function() {
    const $this = $(this);
    if($this.html().replace(/\s|&nbsp;/g, '').length === 0)
        $this.remove();
});

Example: http://jsfiddle.net/ambiguous/7L4WZ/

FCKEditor (not sure about CKEditor or TinyMCE) likes to add <p>&nbsp;</p> to the HTML so you might need the above somewhat ugly approach.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)