Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find and replace HTML entities with jQuery?

Tags:

jquery

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?

like image 853
james6848 Avatar asked Aug 01 '09 16:08

james6848


2 Answers

The simplest thing to do would be

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

Where \u00a0 is the unicode character for  

like image 169
jitter Avatar answered Oct 21 '22 05:10

jitter


try

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

w/o the space after the ampersand.

like image 45
Zed Avatar answered Oct 21 '22 06:10

Zed