Skip to content

Instantly share code, notes, and snippets.

@MichaelVanDenBerg
Created May 28, 2016 14:41
Show Gist options
  • Save MichaelVanDenBerg/0e1cbe4c95bf19ead1c016d2cda6a4f1 to your computer and use it in GitHub Desktop.
Save MichaelVanDenBerg/0e1cbe4c95bf19ead1c016d2cda6a4f1 to your computer and use it in GitHub Desktop.
Remove empty <p> tags with jQuery.
/**
* Remove empty <p> tags.
*
* See: http://stackoverflow.com/questions/27781798/wordpress-retain-formatting-when-calling-extended-content#comment43990361_27782619
* This seems to be the easiest solution. Remove this function if this ever gets fixed.
* Credits: http://stackoverflow.com/questions/6092855/how-do-i-remove-empty-p-tags-with-jquery
*/
( function( $ ) {
$( 'p' ).each( function() {
var $this = $( this );
if ( $this.html().replace( /\s|&nbsp;/g, '' ).length === 0 ) {
$this.remove();
}
});
})( jQuery );
@raihan004
Copy link

how can remove if nested tag also empty
example:

thanks

@msamgan
Copy link

msamgan commented Mar 21, 2019

consider this :

<h2>meet 1</h2><p>hi thre sdsd</p><p></p><p>sadjkasl</p>

now how can i remove

this ?????

@swizzmagik
Copy link

thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment