Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdmwebs/1172174 to your computer and use it in GitHub Desktop.
Save cdmwebs/1172174 to your computer and use it in GitHub Desktop.
Craigslist live filtering
// Adds a live filter box to the results screen on craigslist
// that filters the results that are currently on the page by
// the search term. Like Cmd-F, but better.
// NOTE: Requires dotjs.
if (window.location.pathname.match(/\/search\//) && !$('body').html().match(/Nothing found for that search/)) {
var searchHash = window.location.hash,
searchString = searchHash.split("/")[1],
filterResults = {},
addSearchField = {};
filterResults = function(searchString) {
var searchExpression = new RegExp(searchString, "gi"),
results = $("p.row");
results.show().filter(function() {
if(!$(this).text().match(searchExpression)) { $(this).hide(); }
});
}
addSearchField = function() {
var markup = '| <label for = "result-search">Filter</label> <input type = "text" name = "result-search" id = "result-search" />';
$("blockquote div").first().append(markup);
}
addSearchField();
if (searchString !== "") {
filterResults(searchString);
$('#result-search').val(searchString).focus();
}
$("#result-search").live("keyup", function() {
searchString = $(this).val();
filterResults(searchString);
if (searchString !== "") window.location.hash = "search/" + searchString;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment