0

CSS or jQuery. Is there a selector to set a children class constraint like this?

user3280015
  • 279
  • 2
  • 9

2 Answers2

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
  • 293,187
  • 26
  • 361
  • 361
  • 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
  • @CaseyFalk - That is correct, I'll add that as well – adeneo Jul 28 '14 at 12:13
  • 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
  • 56
  • 5