Remove spaces in image table
I have html like this.
<td id="foo">
<img src="foo.png" /> <img src="foo.png" /> <img src="foo.png" />
</td>
The HTML above contains spaces, and also ,
I want to remove this using jQuery.
How to do it?
already tried with this script, but didn't work.
$('#foo').text().replace(/ /g,'');
+3
4 answers
I think you need to use the content () and then filter out all text nodes that are simple space
var nodes = $("#foo").contents().filter(function() {
return this.nodeType == 3 && /^(\s| )*$/.test(this.nodeValue);
}).remove();
//console.log($("#foo").html());
Perhaps something like this? if you only have images, you can just delete && this.data == " "partI edited it to find spaces and now, the jsfiddle test: http://jsfiddle.net/Rurn2/
+1
- nodeValue , , , . innerHTML , , (, , ). , . , :
$("#foo").contents().filter(function() {
if (this.nodeType == 3) {
return(this.nodeValue.replace(/\s| /g, "") == "");
}
return(false);
}).remove();
, : http://jsfiddle.net/jfriend00/RNTbT/
FYI, this version even processes text nodes, which are a combination of and spaces . It works by removing all spaces and then looking through if something remains. If not, then this is text with a space, node, so it should be deleted.
0