Remove ' ' - still trying Remove ' ' - still trying javascript javascript

Remove ' ' - still trying


You have &nbsp in your code instead of  

$('p').each(function(){    $(this).html($(this).html().replace(/ /gi,''));});

http://jsfiddle.net/genesis/hbvjQ/76/


This one will replace every white-space character:

$('p').text(function (i, old) {    return old.replace(/\s/g, '')});

Or if you only want to replace non-breaking spaces:

$('p').text(function (i, old) {    return old.replace(/\u00A0/g, '')});

jsFiddle Demo

I am setting the new value using a closure as a parameter for .text().


Please note that HTML entities need a closing ; in the end.


Here's a non-jQuery answer, since using jQuery for such a task is overkill unless you're already using it for something else on your site:

var p = document.getElementsByTagName('p');Array.prototype.forEach.call(p, function(el) {  el.innerHTML = el.innerHTML.replace(/ /gi, '');});
<p>No Space</p><p> 1 Space</p><p>  2 Spaces</p><p>   3 Spaces</p><p>    4 Spaces</p>