Reputation: 499
How can I select in jQuery all the div
s that have background-image: url(somepath/somename.png)
in their style?
Upvotes: 4
Views: 2118
Reputation: 8966
Use the filter function:
var matches = $("div").filter(function()
{
return ($(this).css("background-image") == "url('somepath/somename.png')");
});
Upvotes: 1
Reputation: 30498
Try adding a custom selector:
$(document).ready(function() {
$.extend($.expr[':'], {
hasMyImage: function(el) {
return ($(el).css('background-image') == "Url('somepath/somename.png')");
}
});
});
Then to select:
$("div:hasMyImage");
Upvotes: 4
Reputation: 72510
There isn't a jQuery selector, but this might work:
$('div').each( function() {
if ( $(this).css('background-image') == 'url("image.png")' ) {
// do something here
}
});
However, a more efficient method would be to make sure you only have a single class that uses that background image, then simply select $('.bgClass')
Upvotes: 3