jQuery Filter by Data

jQuery won't let you search using a regular selector for data attributes of those data attributes were added using the data function:

jQuery("#something").data("somekey", "someval");
jQuery("#something[data-somekey=someval]"); // does not find it

Instead, define a new function:


jQuery.fn.filterByData = function(prop, val) {
    return this.filter(function() { 
        return jQuery(this).data(prop) == val;
    });
};

jQuery("#something").filterByData("somekey", "someval"); // finds it

StackOverflow question:

http://stackoverflow.com/questions/4191386/jquery-how-to-find-an-element-based-on-a-data-attribute-value

Stackoverflow answer: http://stackoverflow.com/a/15651670/111266