JavaScript How to use JavaScript search on the same page while the user is still typing?

I'm trying to make a JavaScript search for a div within the same page. I'm currently using this code to display my search query:

$('.form-search').on('submit',function(){return false;});
$('.form-search .btn').on('click', function(e){
    var query = $.trim($(this).prevAll('.search-query').val()).toLowerCase();
    $('div.staff-container .bold').each(function(){
         var $this = $(this);
         if($this.text().toLowerCase().indexOf(query) === -1)
             $this.closest('div.staff-container').fadeOut();
        else $this.closest('div.staff-container').fadeIn();
    });
});

Here's a demo of an example with HTML and CSS:

DEMO

So I want to display the div, while the user is still typing. I know there is a refresh function, but I just cannot understand JavaScript. I'm a newbie student.

Answer:1

With a bit of refactoring you can both handle the click on the Search button and updating the search results as you type on the text input. This should work:

function updateResults() {
    var query = $.trim($('.search-query').val()).toLowerCase();
    $('div.staff-container .bold').each(function(){
         var $this = $(this);
         if($this.text().toLowerCase().indexOf(query) === -1)
             $this.closest('div.staff-container').fadeOut();
        else $this.closest('div.staff-container').fadeIn();
    });
}

$('.form-search').on('submit',function(){return false;});
$('.form-search .btn').on('click', updateResults);
$('.search-query').on('input', updateResults);

Updated Jsfiddle: http://jsfiddle.net/1w8L0oeq/

Answer:2

Is that you want ? You just have to listen 'input' event instead of click.

$('.form-search .search-query').on('input', function(e){ ... });

http://jsfiddle.net/817tfr03/

Answer:3

I'm trying to build a simple chrome extension that inserts/hides a div when the browser action (extension icon) is toggled. I've got the basics working but I would like the extension to remain in it's ...

I'm trying to build a simple chrome extension that inserts/hides a div when the browser action (extension icon) is toggled. I've got the basics working but I would like the extension to remain in it's ...

  1. keep chrome extension open
  2. keep chrome extension corrupted
  3. keep chrome extension
  4. keep chrome extension android
  5. google keep chrome extension
  6. keep awake chrome extension
  7. keep awake chrome extension and install
  8. google keep chrome extension not working
  9. keep notes chrome extension
  10. google keep chrome extension page
  11. google keep chrome extension android
  12. keep alive chrome extension
  13. keep private chrome extension
  14. google keep chrome extension download
  15. keep up chrome extension
  16. keep tabs chrome extension
  17. keep key chrome extension
  18. keep safe chrome extension
  19. keep song chrome extension
  20. keepvid chrome extension

I was working with object properties in javascript and I got stuck with a problem that I couldn't find out. In this javascript program I have created object properties for name and age and I have ...

I was working with object properties in javascript and I got stuck with a problem that I couldn't find out. In this javascript program I have created object properties for name and age and I have ...

  1. object defineproperty not working
  2. object fit property not working in ie
  3. object fit property not working

If I manually click on button1 and then on button2 I get the whole html code (410 KiB). However If I try to automate this process with the code below, I only get a part of it, without the html ...

If I manually click on button1 and then on button2 I get the whole html code (410 KiB). However If I try to automate this process with the code below, I only get a part of it, without the html ...

  1. html download generated file
  2. download generated html

I learned that a for iterates on each the array's element, even if a pointed case doesn't exist, while a forEach skips the unexisting cases. Moreover, when a for is writen, a counter must be ...

I learned that a for iterates on each the array's element, even if a pointed case doesn't exist, while a forEach skips the unexisting cases. Moreover, when a for is writen, a counter must be ...

  1. difference between foreach and map
  2. difference between foreach and for loop
  3. difference between foreach and iterator
  4. difference between foreach and for loop javascript
  5. difference between foreach and map javascript
  6. difference between foreach and foreachordered
  7. difference between foreach and for
  8. difference between foreach and foreachremaining
  9. difference between foreach and stream
  10. difference between foreach and enhanced for loop
  11. difference between foreach and filter
  12. difference between foreach and while loop
  13. difference between foreach and for javascript
  14. difference between foreach and for loop c#
  15. difference between foreach and every
  16. difference between foreach and foreachpartition
  17. difference between foreach and map js
  18. difference between foreach and map scala
  19. difference between foreach and forvalues stata
  20. difference between foreach and each