JavaScript - Simple Text Highlight Using jQuery

In this tutorial we will create a Simple Text Higlight Using jQuery. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It is widely used in designing a stunning website. It is an interpreted programming language that has a capabilities of Object-Oriented. So Let's do the coding...

Getting started:

This is the link for the bootstrap that has been used for the layout of the application https://getbootstrap.com/. And this is the link for the jquery that i used in this tutorial https://jquery.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this 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.                 <style>
  7.                         .highlight{
  8.                                 background:yellow;
  9.                                 font-weight:bold;
  10.                         }
  11.                 </style>
  12.         </head>
  13.         <nav class="navbar navbar-default">
  14.                 <div class="container-fluid">
  15.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  16.                 </div>
  17.         </nav>
  18.         <div class="col-md-3"></div>
  19.         <div class="col-md-6 well">
  20.                 <h3 class="text-primary">JavaScript - Simple Text Higlight Using jQuery</h3>
  21.                 <hr style="border-top:1px dotted #ccc;"/>
  22.                 <div class="form-inline">
  23.                         <input type="text" id="search" class="form-control" placeholder="Search here.."/>
  24.                 </div> 
  25.                 <br />
  26.                 <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin eget enim eget nisi gravida semper sed ut turpis. Donec sit amet orci dignissim, dapibus purus eu, fringilla ipsum. Donec varius, augue ac feugiat suscipit, tortor magna mollis enim, id malesuada sapien eros sit amet risus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.</p>
  27.         </div>
  28. <script src="js/jquery-3.2.1.min.js"></script> 
  29. <script src="js/script.js"></script>
  30. </body>
  31. </html>

Creating the Script

This code contains the script of the application. This code will search and highlight the word that have been type.. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js folder.
  1. $(document).ready(function(){
  2.         $('#search').on('keyup', function(){
  3.                 textHiglight($(this).val());
  4.         });
  5. });
  6.  
  7. function textHiglight(search){
  8.  
  9.         if(search){
  10.                 var content = $('p').text();
  11.                 var searchWord = new RegExp(search, "ig");
  12.                 var matches = content.match(searchWord);
  13.                
  14.                 if(matches){
  15.                         $("p").html(content.replace(searchWord, function(match){
  16.                                 return "<span class='highlight'>"+match+"</span>";
  17.                         }));
  18.                 }else{
  19.                         $('.highlight').removeClass('highlight');
  20.                 }
  21.         }else{
  22.                 $('.highlight').removeClass('highlight');
  23.         }
  24. }
There you have it we successfully created a Simple Text Highlight Using 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!

Add new comment