Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-12-2010, 02:18 PM   PM User | #1
many_tentacles
Regular Coder

 
Join Date: Dec 2005
Location: UK
Posts: 207
Thanks: 6
Thanked 2 Times in 2 Posts
many_tentacles is an unknown quantity at this point
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 05:26 PM..
many_tentacles is offline   Reply With Quote
Old 02-12-2010, 02:59 PM   PM User | #2
Spudhead
Senior Coder

 
Spudhead's Avatar
 
Join Date: Jun 2002
Location: London, UK
Posts: 1,856
Thanks: 8
Thanked 110 Times in 109 Posts
Spudhead is on a distinguished road
Code:
$("p").each(function(){
	var $this = $(this);
	if ($this.text() == " "){
		$this.remove();
	}
});
should work
Spudhead is offline   Reply With Quote
Old 02-12-2010, 05:25 PM   PM User | #3
many_tentacles
Regular Coder

 
Join Date: Dec 2005
Location: UK
Posts: 207
Thanks: 6
Thanked 2 Times in 2 Posts
many_tentacles is an unknown quantity at this point
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();
many_tentacles is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:13 PM.


Advertisement
Log in to turn off these ads.