Apply class to parent if child div contains class
# September 11, 2012 at 6:12 pm
Is there any way that anyone knows of to pull this off:
What I want is to change the background of body to blue IF the div inside it has a class of blue. Same principle if it’s red, green, yellow, etc.
Does anyone know of a way to accomplish that?# September 11, 2012 at 6:26 pm
This reply has been reported for inappropriate content.
Yeah – I wish there was a parent selector in CSS but sadly not – Chris did an article on it – http://css-tricks.com/parent-selectors-in-css/
Regarding your problem – TheDoc is pretty much on the money that JQuery has your back.
I just did something for an accordion that adds a class to the parent, relevant part;
You could do something like that.# September 12, 2012 at 12:54 am
Also, it’s generally best practice to make sure you IDs and classes are lowercase. There is *some* preference to using camelCase out there, but I think the industry standard is lowercase with hyphens.
So it your example:
`#ContentRow` would become `#content-row` and `.Blue` would be `.blue`, etc.
You don’t need to add periods when using `.hasClass()`. It also doesn’t play very nicely with multiple class names, they have to be in the proper order. So if you do `.hasClass(‘first second’)` but the element on the page is actually `class=”second first”` then hasClass() will return `false`.
Here’s an example for you: http://jsfiddle.net/e6kE3/# September 12, 2012 at 2:25 am
I work for a company that uses .NET so we’re all about Camel Case. I started off using underscores though.
It’s from a while ago, but still interesting.
You must be logged in to reply to this topic.