How to Highlight HTML Text with jQuery

How to Highlight HTML Text with jQuery


In this tutorial we will create a How to Highlight HTML Text with jQuery. This tutorial purpose is to help you on how to do the search highlight text. This will cover all the basic parts for search highlight text using the jQuery plugin. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is easy to understand just follow my instruction I provided and you can do it without a problem. This program is useful if you want to add an extra feature that can highlight important keyword for your website. I will try my best to give you the simplest way of creating this program Highlight HTML Text with jQuery. So let's do the coding.

Before we get started:

First you have to download the jQuery plugin.

Here is the link for the jQuery that i used in this tutorial

Lastly, this is the link for the template that i used for the layout design

Creating The Interface

This is where we will create a simple interface for our application. This code will display the html form and a sample paragraph. To create this simply copy and write it into your text editor, then save it as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. .highlight{
  7. background:orange;
  8. font-weight:bold;
  9. }
  10. </style>
  11. </head>
  12. <nav class="navbar navbar-default">
  13. <div class="container-fluid">
  14. <a class="navbar-brand" href="">Sourcecodester</a>
  15. </div>
  16. </nav>
  17. <div class="col-md-3"></div>
  18. <div class="col-md-6 well">
  19. <h3 class="text-primary">How to Highlight HTML Text with jQuery</h3>
  20. <hr style="border-top:1px dotted #ccc;"/>
  21. <div class="col-md-4">
  22. <div class="form-group">
  23. <label>Enter a word</label>
  24. <input type="text" id="search" class="form-control" placeholder="Search here.."/>
  25. </div>
  26. </div>
  27. <div class="col-md-8" style="text-indent:50px;">
  28. <p>A boy gets his coat, boots, and pellet gun. He's going to a nearby pond to look for Nethers, an alien species he suspects is planning a kidnapping. Sometimes he shoots the Nethers; sometimes they capture him. While on his quest, he sees something that causes a change in him mission.</p>
  29. </div>
  30. </div>
  31. <script src="js/jquery-3.2.1.min.js"></script>
  32. <script src="script.js"></script>
  33. </body>
  34. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will automatically highlight the text when your enter a word in the search box. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. function highlightText(search){
  3. if(search){
  4. let content = $('p').text();
  5. let searchWord = new RegExp(search, "ig");
  6. let matches = content.match(searchWord);
  8. if(matches){
  9. $("p").html(content.replace(searchWord, function(match){
  10. return "<span class='highlight'>"+match+"</span>";
  11. }));
  12. }else{
  13. $('.highlight').removeClass('highlight');
  14. }
  15. }else{
  16. $('.highlight').removeClass('highlight');
  17. }
  18. }
  20. $(document).ready(function(){
  21. $('#search').on('keyup', function(){
  22. highlightText($(this).val());
  23. });
  24. });

In the given code we first create a method called highlightText(). In the first three line of code we set a certain variables that will have some important functionality. Then will create a conditional statement that will search if the entered word and the word in the paragraph is a matched. When matched the searched word will be highlighted with a background color otherwise it will not be colored.

Lastly, we will call first the basic jQuery ready event to signal that the DOM of the page is now ready to be use. Then bind the input text box with a jQuery function called on() with a condition of keyup, this will track your keyboard press and will dynamically highlight the word that have been search using our method highlightText()


The How to Highlight HTML Text with jQuery source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Highlight HTML Text with jQuery. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment