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
Asked
Active
Viewed 5.8k times
5 Answers
19
$("#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();
}
});
});
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' ]();
});
});

Ian Dunn
- 3,541
- 6
- 26
- 44

Muhammad Talha Akbar
- 9,952
- 6
- 38
- 62
-
sorry man the code i gave you, don't search by clicking search button but do it automatically! – Muhammad Talha Akbar Dec 25 '12 at 14:37
-
+1 Nice answer, but I would also make it case insensitive: `var g = $(this).val().toLowerCase();` and `var s = $(this).text().toLowerCase()` - [updated demo](http://jsfiddle.net/Mottie/ztaz6/2/) – Mottie Dec 25 '12 at 23:25
-
@Mottie I add the link of your fiddle also ;) – Muhammad Talha Akbar Dec 26 '12 at 03:42
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,929
- 14
- 14
-
So... use jQuery just because of `indexOf`, which you don't need jQuery for? – Florian Margaine Dec 25 '12 at 14:28
-
-
This breaks when you search for "fb 1" then change to "fb 3"... it won't unhide "fb 3". Change this: `if (lb.indexOf(v) == -1) { it.hide(); } else { it.show(); }` – Mottie Dec 25 '12 at 23:28
0
Maybe use indexOf method:
var text ="some name";
var search = "some";
if (text.indexOf(search)!=-1) {
// do someting with found item
}

user1276919
- 550
- 5
- 25
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
0
const cities = ['Paris', 'Pattaya', 'New York'];
const searchedItem = 'Pa';
const filteredCities = cities.filter((city) => city.includes(searchedItem));

Mohammad Reza Ghasemi
- 188
- 2
- 11