CSS or jQuery. Is there a selector to set a children class constraint like this?
Asked
Active
Viewed 56 times
0
-
3have you tried anything??? – codingrose Jul 28 '14 at 12:08
-
possible duplicate of [Is there a CSS parent selector?](http://stackoverflow.com/questions/1014861/is-there-a-css-parent-selector) – Casey Falk Jul 28 '14 at 12:16
-
Not a duplicate as such. – Paulie_D Jul 28 '14 at 12:17
-
Not a duplicate since I didn't ask for a CSS only solution. – user3280015 Jul 28 '14 at 12:22
2 Answers
1
You can use jQuery's :has()
Reduce the set of matched elements to those that have a descendant that matches the selector
$('.elements:has(.children)')
This is a jQuery only selector, and it's not available in CSS.
To select elements that only contains elements with a certain class
$('.elements').filter(function() {
return $(this).find('*').length == $(this).find('.children').length;
})

adeneo
- 312,895
- 29
- 395
- 388
-
Note that there currently **isn't a way to select parents based on children with with CSS**. They're thinking about incorporating a `:has()` pseudoselector in the future though (just to fully answer OP's question). – Casey Falk Jul 28 '14 at 12:12
-
-
Will that select divs that **only** contain elements of that class or any parent element that contains an element with that class? I think the former is required (based on the OP's title?). – Paulie_D Jul 28 '14 at 12:14
-
@Paulie_D - It will select elements that contain that class, the question is a bit unclear, but I'll add something. – adeneo Jul 28 '14 at 12:15
-
Cool...I think you'd need some sort of length function to achieve the **only** bit..right? My JS/JQ skills are minimal. – Paulie_D Jul 28 '14 at 12:16
-
Thanks guys, the filter hack seems the best option I get. Yes, the "only" part is required, I don't think the question is unclear. – user3280015 Jul 28 '14 at 12:45
-
@user3280015 - you're welcome, and the question wasn't really unclear, I just didn't get it at first. – adeneo Jul 28 '14 at 12:49
-1
You can use something like this:
parent-element > child-element.class-your-are-after {
//do something here
}
Hope this helps!

lili2311
- 66
- 5
-
Note that OP wants to select the *parent* in the question, @lili2311. – Casey Falk Jul 28 '14 at 12:15
-
No that will select the child elements which is not what the OP is after and in any case, is not a filter. – Paulie_D Jul 28 '14 at 12:15
-