Solved

Edit jquery to show all <li> initially, but show matched

Posted on 2012-04-09
4
551 Views
Last Modified: 2012-08-14
Hi All,

i am using the following jquery to find matching list items:

  jQuery.expr[':'].Contains = function(a,i,m){
      return (a.textContent || a.innerText || "").toUpperCase().indexOf(m[3].toUpperCase())>=0;
  };


  function listFilter(header, list) { // header is any element, list is an unordered list
    // create and add the filter form to the header
    var form = $("<form>").attr({"class":"filterform","action":"#"}),
        input = $("<input>").attr({"class":"filterinput","type":"text"});
    $(form).append(input).appendTo(header);

    $(input)
      .change( function () {
        var filter = $(this).val();
        if(filter) {
          // this finds all links in a list that contain the input,
          // and hide the ones not containing the input while showing the ones that do
          $(list).find("a:not(:Contains(" + filter + "))").parent().slideUp();
          $(list).find("a:Contains(" + filter + ")").parent().slideDown();
        } else {
          $(list).find("li").slideDown();
        }
        return false;
      })
    .keyup( function () {
        // fire the above change event after every letter
        $(this).change();
    });
  }


  //ondomready
  $(function () {
    listFilter($("#header"), $("#list"));
  });
}(jQuery));

Open in new window



It searches each letter from all the displaying <li> tags and then displays the matches.

what i want it to do is make the list items hidden then only show the matches once the user has started typing.

here is it in action:

Any advice is appreciated,

richard
0
Comment
Question by:Richiep86
  • 2
  • 2
4 Comments
 
LVL 38

Accepted Solution

by:
Tom Beck earned 500 total points
ID: 37823142
(function ($) {
  // custom css expression for a case-insensitive contains()
  jQuery.expr[':'].Contains = function(a,i,m){
      return (a.textContent || a.innerText || "").toUpperCase().indexOf(m[3].toUpperCase())>=0;
  };


  function listFilter(header, list) { // header is any element, list is an unordered list
    // create and add the filter form to the header
    var form = $("<form>").attr({"class":"filterform","action":"#"}),
        input = $("<input>").attr({"class":"filterinput","type":"text"});
    $(form).append(input).appendTo(header);
    
    $(list).find("li").css({'display':'none'});
    $(input)
      .change( function () {
        var filter = $(this).val();
        if(filter.length > 0) {
          // this finds all links in a list that contain the input,
          // and hide the ones not containing the input while showing the ones that do
          $(list).find("a:not(:Contains(" + filter + "))").parent().slideUp().css({'display':'none'});
          $(list).find("a:Contains(" + filter + ")").parent().slideDown().css({'display':'block'});
        } else {
          $(list).find("li").css({'display':'none'});
        }
        return false;
      })
    .keyup( function () {
        // fire the above change event after every letter
        $(this).change();
    });
  }


  //ondomready
  $(function () {
    listFilter($("#header"), $("#list"));
  });
}(jQuery));

Open in new window

0
 

Author Closing Comment

by:Richiep86
ID: 37823185
Exactly what i needed - thanks!
0
 

Author Comment

by:Richiep86
ID: 37823378
How easy would it be to add a litte message if it didnt find anything?

not too fond of a javascript alert however...

Would i need to include another 'default' li which would only display if no matches were found?

Thanks

Richard
0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 37823681
Easy enough.

Add this to css:

    .noMatch {
        color:#999;
        font-style:italic
    }

Add this to the list:

<li class="noMatch">no match found</li>

Code:
(function ($) {
  // custom css expression for a case-insensitive contains()
  jQuery.expr[':'].Contains = function(a,i,m){
      return (a.textContent || a.innerText || "").toUpperCase().indexOf(m[3].toUpperCase())>=0;
  };


  function listFilter(header, list) { // header is any element, list is an unordered list
    // create and add the filter form to the header
    var form = $("<form>").attr({"class":"filterform","action":"#"}),
        input = $("<input>").attr({"class":"filterinput","type":"text"});
    $(form).append(input).appendTo(header);
    
    $(list).find("li").css({'display':'none'});
    $(input)
      .change( function () {
        var filter = $(this).val();
        if(filter.length > 0) {
          // this finds all links in a list that contain the input,
          // and hide the ones not containing the input while showing the ones that do
          $(list).find("li a:not(:Contains(" + filter + "))").parent().slideUp().css({'display':'none'});
          $(list).find("li a:Contains(" + filter + ")").parent().slideDown().css({'display':'block'});
          $('.noMatch').css({'display':'none'});       
          $blocks = $('#list li').filter(function(){return $(this).css('display') == 'block';}).length;
          if($blocks == 0){$('.noMatch').css({'display':'block'});}
        } else {
          $(list).find("li").css({'display':'none'});
        } 
        return false;
      })
    .keyup( function () {
        // fire the above change event after every letter
        $(this).change();
    });
  }


  //ondomready
  $(function () {
    listFilter($("#header"), $("#list"));
  });
}(jQuery));

Open in new window

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question