NullVoxPopuli
NullVoxPopuli

Reputation: 65173

how do you eliminate white space from the Dom with hidden unicode characters?

JSFiddle: http://jsfiddle.net/QbyUR/

So, I have an empty TD, that I'm trying to remove the white space from.

Normally I'd do this:

ee.replace(/\s+/, '') == "" // check to see if TD is empty.

the above returns false

but, it wasn't working so I pasted the contents of my TD to a unicode decoder and got this:

U+000D <control> character
U+000A <control> character
U+0009 <control> character
U+0009 <control> character
U+0009 <control> character
U+0009 <control> character
U+0020 SPACE character

or

&#x000d;&#x000a;&#x0009;&#x0009;&#x0009;&#x0009;&#x0020;

The text that I converted is here:

-------


-------

(in between the two lines)

this is what I used: http://software.hixie.ch/utilities/cgi/unicode-decoder/utf8-decoder

I'm 100% certain it's the control characters that are messing me up. And they are bound to be different across all browsers...

How do I get rid of them? is a regex replace going to be sufficient?

Upvotes: 0

Views: 224

Answers (2)

Ry-
Ry-

Reputation: 225074

The real problem here is that you're using innerText, which is not a property on jQuery objects but rather on DOM elements. Simply use the jQuery function .text() instead:

var e = $(".td");
var result = e.text().replace(/\s+/, '') == ""; // check to see if TD is empty

alert(result);​

And everything will work fine. Right now, the string is actually "undefined" because you appended an empty string to a nonexistent property.

Upvotes: 1

mVChr
mVChr

Reputation: 50195

What you are trying to do will work correctly, but you wrote your Javascript wrong. Since you're using jQuery to select the element, you need to get the first index to access the innerText of the element.

http://jsfiddle.net/QbyUR/5/

Upvotes: 0

Related Questions