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. Remove whitespace and line breaks between HTML elements using jQuery

Thanks

1
  • The current answers which remove text-nodes in the thread you mention should work on non-breaking spaces as well.
    – here
    Aug 4, 2014 at 3:13

1 Answer 1

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

demo

4
  • Well this will remove only a single &nbsp; Aug 18, 2010 at 9:41
  • this needs to be iterated for all the occurrences of &nbsp; Aug 18, 2010 at 9:43
  • @ anand I made an update, thanks ;) Aug 18, 2010 at 9:47
  • I think, if the content is dynamic, this won't work, will it?
    – Boo
    Aug 18, 2010 at 11:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.