views:

208

answers:

3

I've written the following functions to remove non-breaking spaces from the html code in a SharePoint site (SharePoint designer litters whatever code you write with non-breaking spaces whenever you open the masterpage or page layouts! You end up spending all your time going back and deleting them).

In any case, it seems overly long, I'm quite new to jQuery and wondered if there is a cleaner way to perform the same task.

<script type="text/javascript">
    $(function(){
            $('h1').each( function() {  
        var $h = $(this);  
        var html = $h.html();  
        html = html.replace( '&nbsp;', '' );  
        $h.html( html );  
    });
}); 
$(function(){
            $('h2').each( function() {  
        var $h = $(this);  
        var html = $h.html();  
        html = html.replace( '&nbsp;', '' );  
        $h.html( html );  
    });
}); 
$(function(){
            $('h3').each( function() {  
        var $h = $(this);  
        var html = $h.html();  
        html = html.replace( '&nbsp;', '' );  
        $h.html( html );  
    });
}); 
$(function(){
            $('h4').each( function() {  
        var $h = $(this);  
        var html = $h.html();  
        html = html.replace( '&nbsp;', '' );  
        $h.html( html );  
    });
}); 
    $(function(){
            $('p').each( function() {  
        var $h = $(this);  
        var html = $h.html();  
        html = html.replace( '&nbsp;', '' );  
        $h.html( html );  
    });
}); 

    </script>
+4  A: 
$(function() {
    $('h2, h3, h4, p').each(function() {
        var $h = $(this);
        var html = $h.html();
        html = html.replace( ' ', '' );
        $h.html( html );
    });
})
Deniz Dogan
This works because jQuery lets you combine selectors into a single selection using commas to separate the selectors. Read http://docs.jquery.com/Selectors/multiple#selector1selector2selectorN for more info.
Programming Hero
+1  A: 
$(function() {
   $('h2, h3, h4, p').html(function(){ return $(this).html().replace(" ", "") }) 
});
Makram Saleh
You are missing `h1` from the selector, and it would look clearer if it was split over a couple of extra lines, but otherwise this is exactly what I would have done.
Alex Barrett
According to the docs, the function param to html would look like `function(idx, oldhtml) { return oldhtml.replace(' ',''); }` --- It claims to empty the element before calling your callback, so the internal `.html()` will be empty.
gnarf
+3  A: 

You can combine all of them into one $(function() { ... }); There is not much reason for you to be wrapping each of those calls in its own document ready event handler.

Also, you are calling the same function on many elements, you can combine your selectors with a , resulting in the much shorter $('h2,h3,h4,h5').each(function() {....}); proposed by others.

Shortening the function body:

var html = $h.html();
html = html.replace('&nbsp;', '');

can be done on one line with:

var html = $h.html().replace('&nbsp;', '');

and since you only reference it long enough to call another function - it doesn't even really need to be stored in its own variable:

$h.html($h.html().replace('&nbsp;', ''));

Or if using jQuery > 1.4 , use the new function syntax:

$h.html(function(index, oldHtml) { return oldHtml.replace('&nbsp;', ''); });

Of course - your method across multiple lines gives you more places to insert debugging code if need be, I usually compress this sort of thing manually after I verify it works.

Making it a plugin

This next approach is a little overkill for the situation, but may shed a little more insight into jQuery and creating 'reusable' code.

// generic replace in html function - proxies all the arguments to 
// .replace()
$.fn.replaceHtml = function() {
  var replaceArgs = arguments;
  // jQuery 1.4 allows
  return this.html(function(idx, oldHtml) {
    return oldHtml.replace.apply(oldHtml, replaceArgs);
  });
  // older versions / another way to write it
  // return this.each(function() { 
  //   var $t = $(this), html = $t.html();
  //   $t.html(html.replace.apply(html, replaceArgs));
  // });
};

// single task that uses the replaceHtml function
$.fn.removeNBSP = function() {
  return this.replaceHtml('&nbsp;', '');
};

$('h1,h2,h3,h4,h5,p').removeNBSP(); 
gnarf