Advertisement
  1. Code
  2. JavaScript
  3. jQuery

Spotlight: jQuery replaceText

Scroll to top
10 min read

Every other week, we'll take an ultra focused look at an interesting and useful effect, plugin, hack, library or even a nifty technology. We'll then attempt to either deconstruct the code or create a fun little project with it.

Today, we're going to take a look at the excellent replaceText jQuery plugin. Interested? Let's get started after the jump.


A Word from the Author

As web developers, we have access to a staggering amount of pre-built code, be it a tiny snippet or a full fledged framework. Unless you're doing something incredibly specific, chances are, there's already something prebuilt for you to leverage. Unfortunately, a lot of these stellar offerings languish in anonymity, specially to the non-hardcore crowd.

This series seeks to rectify this issue by introducing some truly well written, useful code -- be it a plugin, effect or a technology to the reader. Further, if it's small enough, we'll attempt to deconstruct the code and understand how it does it voodoo. If it's much larger, we'll attempt to create a mini project with it to learn the ropes and hopefully, understand how make use of it in the real world.


Introducing replaceText

replaceText Blog postreplaceText Blog postreplaceText Blog post

We're kicking off things by focusing on Ben Alman's excellent replaceText plugin. Here is some quick info:

  • Type: Plugin
  • Technology: JavaScript [Built on the jQuery library]
  • Author: Ben Alman
  • Function: Unobtrusive, concise way to replace textual content

The Problem

Replacing content in your page sounds extremely simple. After all, the native JavaScript method replace seems to do the same thing. If you're feeling particularly lazy, jQuery makes replacing the entire content of the container obscenely easy too.

1
2
// Using just replace

3
$("#container").text().replace(/text/g,'replacement text')
4
5
// Replacing the *entire* content of the container

6
var lazyFool ="entire content with text replaced externally";
7
$("#container").html(lazyFool);

As the saying goes, just because you can do it doesn't really mean you should do. Both these methods are generally shunned [outside of edge cases] because they break a bunch of things whilst doing what they do.

The main issue with these approaches is that they flatten the DOM structure effectively screwing up every non-text node the container holds. If you manage to replace the html itself, using innerHTML or jQuery's html, you'll still unhook every event handler attached to any of its children, which is a complete deal breaker. This is the primary problem this plugin looks to solve.


The Solution

The best way to deal with the situation, and the way the plugin handles it, is to work with and modify text nodes exclusively.

Text nodes appear in the DOM just like regular nodes except that they can't contain childnodes. The text they hold can be obtained using either the nodeValue or data property.

By working with text nodes, we can make a lot of the complexities involved with the process. We'll essentially need to loop through the nodes, test whether it's a text node and if yes, proceed to manipulate it intelligently to avoid issues.

We'll be reviewing the source code of the plugin itself so you can understand how the plugin implements this concept in detail.


Usage

Like most well written jQuery plugins, this is extremely easy to use. It uses the following syntax:

$(container).replaceText(text, replacement);

For example, if you need to replace all occurrences of the word 'val' with 'value', for instance, you'll need to instantiate the plugin like so:

1
2
 $("#container").replaceText( "val", "value" );

Yep, it's really that simple. The plugin takes care of everything for you.

If you're the kind that goes amok with regular expressions, you can do that too!

1
2
 $("#container").replaceText( /(val)/gi, "value" );

You need not worry about replacing content in an element's attributes, the plugin is quite clever.


Deconstructing the Source

Since the plugin is made of only 25 lines of code, when stripped of comments and such, we'll do a quick run through of the source explaining which snippet does what and for which purpose.

Here's the source, for your reference. We'll go over each part in detail below.

1
2
  $.fn.replaceText = function( search, replace, text_only ) {
3
    return this.each(function(){
4
      var node = this.firstChild,
5
        val,
6
        new_val,
7
        remove = [];
8
      if ( node ) {
9
        do {
10
          if ( node.nodeType === 3 ) {
11
            val = node.nodeValue;
12
            new_val = val.replace( search, replace );
13
            if ( new_val !== val ) {
14
              if ( !text_only && /</.test( new_val ) ) {
15
                $(node).before( new_val );
16
                remove.push( node );
17
              } else {
18
                node.nodeValue = new_val;
19
              }
20
            }
21
          }
22
        } while ( node = node.nextSibling );
23
      }
24
      remove.length && $(remove).remove();
25
    });
26
  };

Right, let's do a moderately high level run through of the code.

1
2
 $.fn.replaceText = function( search, replace, text_only ) {};

Step 1 - The generic wrapper for a jQuery plugin. The author, rightly, has refrained from adding vapid options since the functionality provided is simple enough to warrant one. The parameters should be self explanatory -- text_only will be handled a bit later.

1
2
return this.each(function(){});

Step 2 - this.each makes sure the plugin behaves when the plugin is passed in a collection of elements.

1
2
var node = this.firstChild,
3
        val,
4
        new_val,
5
        remove = [];

Step 3 - Requisite declaration of the variables we're going to use.

  • node holds the node's first child element.
  • val holds the node's current value.
  • new_val holds the updated value of the node.
  • remove is an array that will contain node that will need to be removed from the DOM. I'll go into detail about this in a bit.
1
2
if ( node ) {}

Step 4 - We check whether the node actually exists i.e. the container that was passed in has child elements. Remember that node holds the passed element's first child element.

1
2
do{} while ( node = node.nextSibling );

Step 5 - The loop essentially, well, loops through the child nodes finishing when the loop is at the final node.

1
2
if ( node.nodeType === 3 ) {}

Step 6 - This is the interesting part. We access the nodeType property [read-only] of the node to deduce what kind of node it is. A value of 3 implies that is a text node, so we can proceed. If it makes life easier for you, you can rewrite it like so: if ( node.nodeType == Node.TEXT_NODE ) {}.

1
2
val = node.nodeValue;
3
new_val = val.replace( search, replace );

Step 7 - We store the current value of the text node, first up. Next, we quickly replace instances of the keyword with the replacement with the native replace JavaScript method. The results are being stored in the variable new_val.

1
2
if ( new_val !== val ) {}

Step 8 - Proceed only if the value has changed!

1
2
if ( !text_only && /</.test( new_val ) ) {
3
   $(node).before( new_val );
4
   remove.push( node );
5
}

Step 9a - Remember the text_only parameter. This comes into play here. This is used to specify whether the container should be treated as one which contains element nodes inside. The code also does a quick internal check to see whether it contains HTML content. It does so by looking for an opening tag in the contents of new_val.

If yes, the a textnode is inserted before the current node and the current node is added to the remove array to be handled later.

1
2
else {
3
         node.nodeValue = new_val;
4
        }

Step 9b - If it's just text, directly inject the new text into the node without going through the DOM juggling hoopla.

1
2
remove.length && $(remove).remove();

Step 10 - Finally, once the loop has finished running, we quickly remove the accumulated nodes from the DOM. The reason we're doing it after the loop has finished running is that removing a node mid-run will screw up the loop itself.


Project

The small project we're going to build today is quite basic. Here is the list of our requirements:

  • Primary requirement: Applying a highlight effect to text that's extracted from user input. This should be taken care of completely by the plugin.
  • Secondary requirement: Removing highlight on the fly, as required. We'll be drumming up a tiny snippet of code to help with this. Not production ready but should do quite well for our purposes.

Note: This is more of a proof of concept than something you can just deploy untouched. Obviously, in the interest of preventing the article from becoming unweildy, I’ve skipped a number of sections that are of utmost importance for production ready code -- validation for instance.

The actual focus here should be on the plugin itself and the development techniques it contains. Remember, this is more of a beta demo to showcase something cool that can be done with this plugin. Always sanitize and validate your inputs!


The Foundation: HTML and CSS

1
2
<!DOCTYPE html>  
3
<html lang="en-GB">  
4
	<head>
5
		<title>Deconstruction: jQuery replaceText</title>
6
		<link rel="stylesheet" href="style.css" />
7
	</head>
8
9
	<body>
10
    	<div id="container">
11
        	<h1>Deconstruction: jQuery replaceText</h1>
12
		<div>by Siddharth for the lovely folks at Nettuts+</div>
13
		
14
		<p>This page uses the popular replaceText plugin by Ben Alman. In this demo, we're using it to highlight arbitrary chunks of text on this page. Fill out the word, you're looking for and hit go. </p>
15
		
16
		<form id="search"><input id="keyword" type="text" /><a id="apply-highlight" href="#">Apply highlight</a><a id="remove-highlight" href="#">Remove highlight</a></form>
17
		<p id="haiz"> <-- Assorted text here --></div>
18
	<script src="js/jquery.js"></script>
19
	<script src="js/tapas.js"></script>
20
21
	</body>
22
</html>

The HTML should be pretty explanatory. All I've done is create a text input, two links to apply and remove the highlight as well as a paragraph containing some assorted text.

1
2
body{
3
	font-family: "Myriad Pro", "Lucida Grande", "Verdana", sans-serif;
4
	font-size: 16px;
5
}
6
7
p{
8
	margin: 20px 0 40px 0;
9
}
10
11
12
h1{
13
	font-size: 36px;
14
	padding: 0;
15
	margin: 7px 0;
16
}
17
18
h2{
19
	font-size: 24px;
20
}
21
22
#container{
23
	width: 900px;
24
	margin-left: auto;
25
	margin-right: auto;
26
	padding: 50px 0 0 0;
27
	position: relative;
28
}
29
30
#haiz { 
31
	padding: 20px; 
32
	background: #EFEFEF; 
33
	-moz-border-radius:15px;
34
	-webkit-border-radius: 15px;
35
	border: 1px solid #C9C9C9; 
36
}
37
38
#search {
39
	width: 600px; 
40
	margin: 40px auto; 
41
	text-align: center; 
42
}
43
44
#keyword { 
45
	width: 150px; 
46
	height: 30px; 
47
	padding: 0 10px; 
48
	border: 1px solid #C9C9C9; 
49
	-moz-border-radius:5px;
50
	-webkit-border-radius: 5px;
51
	background: #F0F0F0;
52
	font-size: 18px;
53
}
54
55
#apply-highlight, #remove-highlight { 
56
	padding-left: 40px; 
57
}
58
59
.highlight { 
60
	background-color: yellow;
61
}

Again, pretty self explanatory and quite basic. The only thing to note is the class called highlight that I'm defining. This will be applied to the text that we'll need to highlight.

At this stage, your page should look like so:

Tutorial imageTutorial imageTutorial image

The Interaction: JavaScript

First order of the day is to quickly hook up our link with their handlers so the text is highlighted and unhighlighted appropriately.

1
2
var searchInput = $("#keyword"), 
3
      searchTerm, 
4
      searchRegex;  
5
$("#apply-highlight").click(highLight);
6
$("#remove-highlight").bind("click", function(){$("#haiz").removeHighlight();});

Should be fairly simple. I declare a few variables for later use and attach the links to their handlers. highLight and removeHighlight are extremely simple functions we'll look at below.

1
2
function highLight() { 
3
   searchTerm = searchInput.val();
4
   searchRegex  = new RegExp(searchTerm, 'g');
5
   $("#haiz *").replaceText( searchRegex, '<span class="highlight">'+searchTerm+'</span>');
6
}
  • I've chosen to create a vanilla function, and not a jQuery plugin, because I'm lazy as a pile of rocks. We start off by capturing the input box's value.
  • Next up, we create a regular expression object using the search keyword.
  • Finally, we invoke the replaceText plugin by passing in the appropriate values. I'm choosing to directly include searchTerm in the markup for brevity.
1
2
jQuery.fn.removeHighlight = function() {
3
   return this.find("span.highlight").each(function() {
4
      with (this.parentNode) {
5
         replaceChild(this.firstChild, this);
6
      }
7
 })
8
};

A quick and dirty, hacky method to get the job done. And yes, this is a jQuery plugin since I wanted to redeem myself. The class is still hardcoded though.

I'm merely looking for every span tag with a class of highlight and replacing the entire node with the value it contains.

Before you get your pitchforks ready, remember that this is just for demonstration purposes. For your own application, you'll need a much more sophisticated unhighlight method.


Wrapping Up

And we're done. We took a look at an incredibly useful plugin, walked through the source code and finally finished by creating a mini project with it.

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.