8

I've constructed a calendar template for a Drupal site using an HTML table, and I've got jQuery to add a class 'no-text' to each empty cell:

$('table.calendar td:empty').addClass('no-text');

This works well, but my problem is that the CMS WYSIWYG editor automatically adds the HTML entity   to empty cells. I've therefore attempted to find and replace the entities with a 'real' space beforehand, but jQuery fails to find them:

$('table.calendar td').each(function() {
   var $this = $(this);
   var t = $this.text();
   $this.text(t.replace('[entity here]',''));
});

This snippet works fine when replacing a normal string, but the   seems to be something different!

So my question is this: how can jQuery be used to search and replace HTML entities?

1
  • voting up, as it's a good question. nicely asked. Commented Aug 1, 2009 at 16:58

5 Answers 5

9

The simplest thing to do would be

$this.text(t.replace('\u00a0',''));

Where \u00a0 is the unicode character for  

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for all the answers, folks. This one worked just fine - cheers!
2

try

replace(/& nbsp;/g, ''); 

w/o the space after the ampersand.

Comments

1

Have you tried .html() ?

$this.html('');

Comments

1

If your nbsp is within a tag, rather than an external js file, the html needs to be encoded twice:

 

Comments

1

This is another alternative that works.

var nbsp = unescape("%a0");     // a0 is hex code point for  
$this.text(t.replace(nbsp,''));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.