views:

514

answers:

3

If I want to select every image which it's alt is Home for example, I can do something like this:

$("img[alt='Home']")

But how can I select every elements which their width CSS property is 750px for example in a single selector?

EDIT: If there is no such selector, is there any plugin, or any plans to do it in the next jQuery versions?

+6  A: 
var $images = $("img").filter(function() {
    return $(this).css('width') == '750px';
});

EDIT: There is no plugin I am aware of, or any plans to include such specific functionality. You can easily pluginify it yourself, such as (untested):

$.fn.filterByWidth = function(width) {
    var $images = $("img").filter(function() {
        return $(this).css('width') == width;
    });
    return $images;
};

Usage:

$images = $('#div img').filterByWidth('750px');
$images = $('#div img').filterByWidth('50%');
...etc...
karim79
Isn't this possible with a single simple selector?
TTT
Should use `width()` instead
Josh Stodola
@Alon - I seriously doubt it
karim79
@josh question specifically asked for css declared width only
cobbal
Ahhh, good call. Sorry about that.
Josh Stodola
I meant if is there any way to add this functionally so you can do:img[Attribute='Value']{CSSProperty='CSSValue'}
TTT
I'm not sure if I misread the question, but I don't think the OP wants to find images only, nor images with a specific alt attribute. They said "every element".
DisgruntledGoat
@DisgruntledGoat - I think you're right, I've taken away the attribute filter.
karim79
+2  A: 

I have no idea if this will work, but...:

${"[style*=width: 750px]")

However, you might be better off using a class to control the width, then modifying the width of all instances of that class... or changing to a different class:

$(".classname").removeClass("classname").addClass("otherclassname");
R. Bemrose
@R. Bemrose - I thought that would work when I saw your post, but I tried it in several scenarios with no luck whatsoever. I believe you can do something similar in Prototype, it's too bad that you can't in jQuery.
karim79
+4  A: 

Not necessarily a great idea, but you could add a new Sizzle selector for it :

$.expr[':'].width = function(elem, pos, match) {
    return $(elem).width() == parseInt(match[3]);
}

which you could then use like so:

$('div:width(970)')

That's going to be horrifically slow, though, so you'd want to narrow down on the number of elements you're comparing with something like :

$('#navbar>div:width(970)')

to only select those divs that are direct descendants of the navbar, which also have a width of 970px.

jdelStrother
Is `$.expr` the correct API in jQuery? I just used `jQuery.find.selectors.filters` because that's where Sizzle is exposed to the global scope.
eyelidlessness
Removed my answer as it more or less duplicates yours. Still unclear about the API though.
eyelidlessness
Ahh. They evaluate to the same object, but jQuery.find.selectors.filters is much less silly looking. Thanks for pointing that out
jdelStrother