I have a father div with X number of child divs. What I'm trying to do is to loop throught all the child elements and alternate some styles. For example (having 4 child divs)
- Child 1 style = nothing;
- Child 2 style = font-size: 24px;
- Child 3 style = nothing;
- Child 4 style = font-size: 24px;
At the moment this is what I have:
$('#fatherDiv > div').each(function() {
if ($(this).css("background-color") !== 'red') {
$(this).css("background-color", "red");
}
});
With this code I'm able to loop throught all the childs but it seems that the if condition is wrong. I guess that I need to the the comparision but with the previous child instead of the current, so how can I do that? And is this #div > div.each the best way to loop throught the elements?
Here is a fiddle to play with.