# datafeedr-api/1.0.21/js/searchfilter.js

Datafeedr API, version 1.0.21. 51 lines.

- Page: https://pluginprobe.com/plugins/datafeedr-api/1.0.21/code/js/searchfilter.js
- Raw: https://pluginprobe.com/plugins/datafeedr-api/1.0.21/raw/js/searchfilter.js
- Modified: 2014-02-02T18:44:36+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/datafeedr-api/1.0.21/code/js/searchfilter.js#L10-L20`.

```javascript
(function($) {

    var sel = function(ptr, context) {
        if($.isFunction(ptr))
            return ptr.apply(context);
        return typeof ptr == "string" ? context.find(ptr) : ptr;
    };

    var filter = function(obj, search, opts) {
        var re = new RegExp(search.replace(/[.\\\\+*?\[\]\{\}\(\)-]/g, "\\$&"),
            opts.caseSensitive ? "" : "i");
        var ctx = sel(opts.element, obj);

        ctx.each(function() {
            var e = $(this);

            var box = opts.subject ? sel(opts.subject, e) : this;
            var val = box.text();

            if(!search.length) {
                e.show();
                if(opts.highlight) {
                    box.html(val);
                }
            } else if(val.match(re)) {
                e.show();
                if(opts.highlight) {
                    box.html(val.replace(re, opts.highlightRe));
                }
            } else {
                e.hide();
            }

        });

        if(opts.after)
            opts.after.apply(ctx);

    };

    $.fn.searchFilter = function(opts) {
        if(opts.highlight) {
            opts.highlightRe = $("<div>@</div>").wrapInner(opts.highlight).html().replace(/@/g, "$$&");
        }
        $(this).keyup(function() { filter(this, this.value, opts) });
        $(this).change(function() { filter(this, this.value, opts) });
    }


})(window.jQuery);

```
