191

This apparently is not working:

X = $td.text();
if (X == ' ') {
  X = '';
}

Is there something about a non-breaking space or the ampersand that JavaScript doesn't like?

5
  • 1
    Not working how? What are you then doing with X? Commented Mar 8, 2011 at 20:33
  • Oh, if I say X=$td.html() it works. Commented Mar 8, 2011 at 20:34
  • 8
    Remember that .text() strips out markup, thus I don't believe you're going to find   in a non-markup result. Commented Mar 8, 2011 at 20:34
  • 1
    I may be wrong here, but doesn't text() expand html entities? Commented Mar 8, 2011 at 20:35
  • \u00A0 - unicode nbsp \x20 - ascii space <p>&nbsp;P1nbsp</p> In browser console: /\u00A0/.test($0.childNodes[0].nodeValue[0]) Display "true" Commented Apr 8, 2019 at 6:43

4 Answers 4

397

&nbsp; is a HTML entity. When doing .text(), all HTML entities are decoded to their character values.

Instead of comparing using the entity, compare using the actual raw character:

var x = td.text();
if (x == '\xa0') { // Non-breakable space is char 0xa0 (160 dec)
  x = '';
}

Or you can also create the character from the character code manually it in its Javascript escaped form:

var x = td.text();
if (x == String.fromCharCode(160)) { // Non-breakable space is char 160
  x = '';
}

More information about String.fromCharCode is available here:

fromCharCode - MDC Doc Center

More information about character codes for different charsets are available here:

Windows-1252 Charset
UTF-8 Charset

1
  • Ordinary spaces are breaking, so it is '\x20', but better to use ' '. Commented May 2, 2019 at 18:28
8

Remember that .text() strips out markup, thus I don't believe you're going to find &nbsp; in a non-markup result.

Made in to an answer....

var p = $('<p>').html('&nbsp;');
if (p.text() == String.fromCharCode(160) && p.text() == '\xA0')
    alert('Character 160');

Shows an alert, as the ASCII equivalent of the markup is returned instead.

1
  • 2
    Using jQuery we can see that $("<div>&nbsp;</div>").text().charCodeAt() gives 160 (unicode for nbsp)
    – cobbal
    Commented Mar 8, 2011 at 20:38
2

That entity is converted to the char it represents when the browser renders the page. JS (jQuery) reads the rendered page, thus it will not encounter such a text sequence. The only way it could encounter such a thing is if you're double encoding entities.

0

The jQuery docs for text() says

Due to variations in the HTML parsers in different browsers, the text returned may vary in newlines and other white space.

I'd use $td.html() instead.

1
  • This will add html tags to your page 's markup. For example, the string '<text>' will be an html element. Commented Dec 13, 2019 at 8:01

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