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:


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