Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using JavaScript / jQuery to Search Keywords

I have a static site and I was wondering if it is possible to have a search form on the website and then use JavaScript of jQuery to search the page for certain keywords and then use jQuery to add a class or something.

like image 709
user1658756 Avatar asked Oct 16 '12 17:10

user1658756


1 Answers

use the JQuery "Contains" Selector

  • http://api.jquery.com/contains-selector/
  • http://www.w3schools.com/jquery/sel_contains.asp

Then use the addClass() method to assign your class

  • http://api.jquery.com/addClass/

    <html>
    <head>
      <script src="http://code.jquery.com/jquery-latest.js"></script>
    </head>
    <body>
    
    <div>John Resig</div>
    
    <div>George Martin</div>
    <div>Malcom John Sinclair</div>
    <div>J. Ohn</div>
    
    
    <script>
    $("div:contains('John')").css("text-decoration", "underline");
        </script>
    
    </body>
    </html>
    
like image 176
Bryan Allo Avatar answered Sep 23 '22 17:09

Bryan Allo