JavaScript - Find And Highlight Search Text

Submitted by: 
Language: 
Visitors have accessed this post 263 times.

In this tutorial we will create a Find And Highlight Search Text Using JavaScript. This code will highlight the important string base on the inputted keyword in the text box. The code use jQuery plugin .on('keyup') function to trigger a certain method that will highlight a string and mark a background color on it using RegExp() by adding pattern and attributes.

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. .highlight{
  7. background:lightblue;
  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="<a href="https://sourcecodester.com">Sourcecodester</a>
  15. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  16. </a> </div>
  17. </nav>
  18. <div class="col-md-3"></div>
  19. <div class="col-md-6 well">
  20. <h3 class="text-primary">JavaScript - Find And Highlight Search Text</h3>
  21. <hr style="border-top:1px dotted #ccc;"/>
  22. <div class="col-md-4">
  23. <div class="form-group">
  24. <label>Search here..</label>
  25. <input type="text" id="keyword" class="form-control"/>
  26. </div>
  27. </div>
  28. <div class="col-md-8">
  29. <p>It appeared that the Traveller had responded to the invitation of the Commandant only out of politeness, when he had been asked to attend the execution of a soldier condemned for disobeying and insulting his superior. Of course, interest in the execution was not very high even in the penal colony itself.</p>
  30. </div>
  31. </div>
  32. <script src="js/jquery-3.2.1.min.js"></script>
  33. <script src="js/script.js"></script>
  34. </body>
  35. </html>

Creating the Script

This code contains the script of the application. This code will find and highlight a string that have been inputted. 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. function searchHighligh(keyword){
  2.  
  3. if(keyword){
  4. var content = $('p').text();
  5. var searchText = new RegExp(keyword, "ig");
  6. var matches = content.match(searchText);
  7.  
  8. if(matches){
  9. $("p").html(content.replace(searchText, 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. }
  19.  
  20. $(document).ready(function(){
  21. $('#keyword').on('keyup', function(){
  22. searchHighligh($(this).val());
  23. });
  24. });

There you have it we successfully created a Find And Highlight Search Text using JavaScript. 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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.