Use jQuery to Replace a Word with a Link

If you use a word multiple times on your site and you don’t feel like going through all your posts to replace every instance of it, you can use jQuery to search your page and do the replacing for you. I wanted to replace a word throughout an entire site with a link and all it took was a small piece of code.
If you don’t already have jQuery loaded into your page, include this before your closing <head>.


That will load up jQuery from Google.
Now you just need to include this in your site’s footer, before the closing <body>. (Why in the footer? Because including your JavaScript after all of your code is the efficient way to do it.)


Since jQuery doesn’t have a core function for replacing text, we will use the JavaScript replace() function. The i after the slash makes the search and replace case-insensitive, and the g makes it global.
I have the script working on this page to replace every instance of the word jQuery with a link to the jQuery Web site.

Share this:

Email
Facebook
Twitter
Pinterest
Pocket

Premium Themes for WordPress

Looking for an easy-to-use Premium Theme for WordPress? Check out Themes by bavotasan.com and have your site up and running in no time.

Use this WordPress website builder to build powerful websites in no time for your or your clients.

WordPress Hosting

WP Engine – designed from the ground-up to support and enhance the workflow of web designers.

Bluehost – providing quality web hosting solutions since 1996.

About the author

Picture of Luke Perrie

Luke Perrie