jquery replace text method

by gschutz

HTML

<label>Search:
  <input id="search-box" type="text"/>
</label>

JavaScript

(function($) {

    $.fn.replaceText = function(text, newText) {
        this.contents().each(function(){
            if (this.nodeType === 3) {
                this.textContent = this.textContent.replace(text, newText);
            }
        });
        return this;
    };
    
}(jQuery));

$('label').replaceText('Search', 'Quick Search');