JavaScript - Simple Text Highlight Using jQuery

Submitted by: 
Visitors have accessed this post 88 times.

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

And this is the link for the jquery that i used in this tutorial

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:yellow;
  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="">Sourcecodester</a>
  15. " rel="nofollow">">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 - 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. });
  7. function textHiglight(search){
  9. if(search){
  10. var content = $('p').text();
  11. var searchWord = new RegExp(search, "ig");
  12. var matches = content.match(searchWord);
  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

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.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.