Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In-page search using contains() to show/hide div content

I am trying to add a search functionality to my FAQ page and am absolutely stuck.

What I want is a text box where the user inputs a keyword(or words), that runs a jquery for the keyword and sets display:block for all the relevant answers.

What I have so far is this:

    <form name="searchBox">
       Keyword(s): <input type="text" name="keyword" />
       <input type="button" value="Search" onClick="searchFunction()" />
    </form>
    <div class="searchable" style="display:none">
       This is the first software question and answer.</div>
    <div class="searchable" style="display:none">
       This is the first hardware question and answer.</div>
    <script type="text/javascript">
       function searchFunction() {
          var searchTerm = document.searchBox.keyword.value;
          $(" :contains('"+searchTerm+"')").addStyle("display:block"); }
    </script>
like image 357
FrazzleSnazzle Avatar asked Dec 21 '22 11:12

FrazzleSnazzle


2 Answers

Try this

function searchFunction() {
          var searchTerm = document.searchBox.keyword.value;
          $(".searchable").each(function(){
              $(this).(":contains('"+searchTerm+"')").show(); 
           });

}
like image 141
ShankarSangoli Avatar answered Dec 24 '22 02:12

ShankarSangoli


Try this.

function searchFunction() {
    $(".searchable")
        .hide()
        .filter(":contains('" + $("input[name='keyword']").val() + "')")
        .show();
}
like image 34
naveen Avatar answered Dec 24 '22 01:12

naveen