Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Dec 2005
    Location
    UK
    Posts
    207
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Removing empty paragraphs

    Hi

    I have this code to remove any paragraphs which do not have any content, but it does not work if there is a space in the paragraph.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Untitled Document</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
    <script type="text/javascript">
    	$(document).ready(function(){	
    		$("p:empty").remove();
    	});
    </script>
    </head>
    
    <body>
    
    
    <p class="1">111</p>
    
    <p class="2">2222</p>
    
    <p class="3"></p>
    
    <p class="4"> </p>
    
    </body>
    </html>
    Paragraph 4 does not get removed... Does anyone know how i can trim any spaces from the beginning of every paragraph then remove the empty ones.

    Thanks
    Last edited by many_tentacles; 02-12-2010 at 06:26 PM.

  • #2
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts
    Code:
    $("p").each(function(){
    	var $this = $(this);
    	if ($this.text() == " "){
    		$this.remove();
    	}
    });
    should work

  • #3
    Regular Coder
    Join Date
    Dec 2005
    Location
    UK
    Posts
    207
    Thanks
    6
    Thanked 2 Times in 2 Posts
    That worked if you could guarantee there would only ever be 1 space. Any more and it didn't work.

    This did though.

    Thanks for your response... It's all helping me get to grips with jquery.

    Code:
    $("p").filter(function() {
            	return $.trim($(this).text()) === "";
            }).remove();


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •