views:

70

answers:

1

How can I use jquery to remove non-breaking spaces (nbsp) which appear between html elements?

So, for example, the following code is generated by a cms:

<div><span>content</span>&nbsp;<span>content</span></div>

I am able to target the elements using slectors.

The following page suggests an answer to remove normal whitespace, but not non-breaking spaces. http://stackoverflow.com/questions/1539367/remove-whitespace-and-line-breaks-between-html-elements-using-jquery

Thanks

+1  A: 
var div = "<div><span>content</span>&nbsp;<span>content</span></div>";    
var newdiv = div.replace(/&nbsp;/g,'');

demo

Reigel
Well this will remove only a single  
anand
this needs to be iterated for all the occurrences of  
anand
@ [anand](http://stackoverflow.com/users/380829/anand) I made an update, thanks ;)
Reigel
I think, if the content is dynamic, this won't work, will it?
Boo