9

Here I write code where all persons names comes from Facebook API, and it is showing on lightbox. Now I want to implement search functionality using JavaScript/jQuery. Can you help me? How should I implement search function?

Invite Facebook Friend James Alan Mathew

Image

Brian Tompsett - 汤莱恩
  • 5,438
  • 68
  • 55
  • 126
Rushikesh jogle
  • 581
  • 2
  • 9
  • 28

4 Answers4

18
$("#search-criteria").on("keyup", function() {
    var g = $(this).val();
    $(".fbbox .fix label").each( function() {
        var s = $(this).text();
        if (s.indexOf(g)!=-1) {
            $(this).parent().parent().show();
        }
        else {
            $(this).parent().parent().hide();
        }
    });
});​

Working Fiddle

or Better Way:

$("#search-criteria").on("keyup", function() {
    var g = $(this).val().toLowerCase();
    $(".fbbox .fix label").each(function() {
        var s = $(this).text().toLowerCase();
        $(this).closest('.fbbox')[ s.indexOf(g) !== -1 ? 'show' : 'hide' ]();
    });
});​

Working Fiddle

Ian Dunn
  • 3,431
  • 6
  • 25
  • 40
Muhammad Talha Akbar
  • 9,654
  • 6
  • 37
  • 61
1

Use Jquery

​  $(document).ready(function(){

   var search = $("#search-criteria");
   var items  = $(".fbbox");

   $("#search").on("click", function(e){

        var v = search.val().toLowerCase();
       if(v == "") { 
           items.show();
           return;
       }
        $.each(items, function(){
            var it = $(this);
            var lb = it.find("label").text().toLowerCase();
            if(lb.indexOf(v) == -1) 
                 it.hide();
        });
    });        
});​

Demo : http://jsfiddle.net/C3PEc/2/

Andy Ecca
  • 1,819
  • 14
  • 13
0

Maybe use indexOf method:

var text ="some name";
var search = "some";

if (text.indexOf(search)!=-1) {

    // do someting with found item

}
user1276919
  • 508
  • 3
  • 24
0

You can use regular expression instead of indexOf as it may not work in IE7/IE8 and using regular expression you will can also use the 'i' modifier to make the search case insensitive.

Thanks

user1654525
  • 121
  • 6