Using jQuery, how would you find elements which have a particular style (eg: float: left
), regardless of whether it's an inline style or one defined in a CSS file?
views:
275answers:
3
+14
A:
Using the filter function:
$('*').filter(function() {
return $(this).css('float') == 'left';
});
Replace '*' with the appropriate selectors for your case.
Eran Galperin
2009-01-14 06:20:18
Much better solution than mine I think.
Genericrich
2009-01-14 06:23:33
Yeah, +1 good answer, but for sanitys and speeds sake one may want to reduce the search field to something less than '*'
Kent Fredric
2009-01-14 06:52:31
Of course he should pass the selectors relevant for him :)
Eran Galperin
2009-01-14 07:05:24
A:
Well, I don't know if I would approach this this way. Why not just rename those elements with a consistent ID or class name, and select that?
If that's not an option, this should work:
this.getElementsByTagName('div').item(0).style.float = 'left';
I think.
Genericrich
2009-01-14 06:22:43
This will not work. Your code selects all divs, reduces selection to the first div, then sets its float to left.
yaauie
2009-01-14 08:23:50
+1
A:
This is gonna be slow. Like REALLY slow. If you know you need to select all elements with a given CSS style, you will see much better performance by applying a single additional css rule to each element, and then selecting by that rule.
It's going to be MUCH faster and more readable to boot.
CSS:
.float-left {float:left}
Javascript:
$('.float-left');
Triptych
2009-01-14 06:25:26