3

I have html like this.

<td id="foo">
    <img src="foo.png" /> <img src="foo.png" /> <img src="foo.png" />&nbsp; 
</td>

The HTML above contains whitespace and also &nbsp;,
I want to remove that using jQuery.
How to do that?

already tried with this script but not work.

$('#foo').text().replace(/ /g,'');
GusDeCooL
  • 5,639
  • 17
  • 68
  • 102
  • 1
    Possible dup http://stackoverflow.com/questions/1539367/remove-whitespace-and-line-breaks-between-html-elements-using-jquery – elclanrs Apr 11 '12 at 07:37

4 Answers4

1

I think you need to use contents() and then filter out all the textNodes that is a plain space

var nodes = $("#foo").contents().filter(function() {
    return this.nodeType == 3 && /^(\s|&nbsp;)*$/.test(this.nodeValue);
}).remove();

//console.log($("#foo").html());

something like that maybe? if you only got images then you can just remove && this.data == " " part

I have edited it looks for spaces and   now, jsfiddle test: http://jsfiddle.net/Rurn2/

voigtan
  • 8,953
  • 2
  • 29
  • 30
  • you may want to add for linebreaks: `/^(\s| |\n)*$/` also to the regex if you also want empty spaces+html spaces and newlines to be removed  \n for example wont be removed now – voigtan Apr 11 '12 at 08:16
0

Have you tried this: $('#foo').html($('#foo').html().replace(/ /g,''));? your code will return a white-space-less string, based on the text in the element. But if you want to overwrite the text of the element, you have to enter the new string in between the braces.

Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
  • 1
    You can't remove all whitespace from the innerHTML because this will remove the whitespace from inside the HTML tags. – jfriend00 Apr 11 '12 at 07:54
  • nips, forgot about that white-space :D... silly. I'd suggest using the other answers here matching html entities, or create a temporary element (`document.createElement`), fill it with the html, so that the entities are converted to spaces, and use that parsed string. It will require a bit more code that way, but it's (generally speaking) a safer way to decode/encode html entities. If all you want is ` `, the answer jfriend00 provided will work just fine, though – Elias Van Ootegem Apr 11 '12 at 08:09
0

Try this:

var html = '>' + $('#foo').html() + '<';
html = html.replace(/&nbsp;/g, '').replace(/>\s+</gm, '><');
$('#foo').html(html.substring(1, html.length-1));
Claude Vedovini
  • 2,421
  • 21
  • 19
0

The safest way is to find all the text nodes and compare their nodeValue to exactly what you're looking for and remove the ones that contain only whitespace. Doing operations on the innerHTML directly is fraught with difficulties because you have to make sure you aren't messing up things inside the actual tags (e.g. whitespace that is supposed to be there). Operating on the text nodes only uses the browser to keep you safe. You can find all the text nodes that match your condition like this:

$("#foo").contents().filter(function() {
    if (this.nodeType == 3) {
        return(this.nodeValue.replace(/\s|&nbsp;/g, "") == "");
    }
    return(false);
}).remove();​

You can see it work here: http://jsfiddle.net/jfriend00/RNTbT/

FYI, this version will even handle text nodes that are a combination of whitespaces and &nbsp;. It works by removing all whitespace and &nbsp; and then seeing if there's anything left. If not, then it's a whitespace-only text node so it should be removed.

jfriend00
  • 683,504
  • 96
  • 985
  • 979