Skip to content

Instantly share code, notes, and snippets.

@oojacoboo
Created December 17, 2012 03:20
Show Gist options
  • Save oojacoboo/4315573 to your computer and use it in GitHub Desktop.
Save oojacoboo/4315573 to your computer and use it in GitHub Desktop.
$.fn.setupFilterInput = function(callback) {
var $input = $(this),
$searchX = $input.parent().find("#x-icon");
$input.focus(function(event) {
if(this.value == $input.prop("placeholder"))
this.value = "";
}).blur(function(event) {
if(!this.value || this.value == "")
this.value = $input.prop("placeholder");
}).keyup(function(event) {
if(this.value.length > 0)
$searchX.addClass("active");
else
$searchX.removeClass("active");
if(_.isFunction(callback))
$input.on("keyup", callback);
}).trigger("blur").end();
$searchX.click(function(event) {
event.preventDefault();
$input.val($input.prop("placeholder"));
$(this).removeClass("active");
});
return this;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment