Change an element's background if another is visble

Change an element's background if another is visble

I have multiples of ".container" and They're all shown one at a time. I'm trying to get the backgroundof the parent DIV to change depending on the visible ".container" . but it doesn't work. Any ideas on how this can be achieved?


HTML

  1. <div id="main">
  2. <div class="prev">Previous</div>
  3. <div class="next">Next</div>
  4. <div class="container active">One</div>
  5. <div class="container">Two</div>
  6. <div class="container">Three</div>
  7. <div class="container">Four</div>

  8. </div>
CSS
  1. .prev, .next {
  2. float: left;
  3. color:blue;
  4. font-size: 24px;
  5. margin: 10px 0 50px 50px;
  6. cursor:pointer;
  7. width: 40%;
  8. color: black;
  9. }
  10. .container {
  11. width: 100%;
  12. font-size: 70px;
  13. margin-bottom: 20px;

  14. }
  15. .container {
  16. display: none;
  17. }
  18. .container.active {
  19. display: inherit;
  20. }


jQuery
  1. $(".prev").on("click", function(){
  2. $(".container.active").removeClass("active").prev().addClass("active");
  3. var $prev = $('.container.active').removeClass('active');
  4. if ($prev.length) {
  5. $prev.addClass('active');
  6. }
  7. else {
  8. $(".container:last").addClass('active');
  9. }

  10. });
  11. $(".next").on("click", function(){
  12. $(".container.active").removeClass("active").next().addClass("active");
  13. var $next = $('.container.active').removeClass('active');
  14. if ($next.length) {
  15. $next.addClass('active');
  16. }
  17. else {
  18. $(".container:first").addClass('active');
  19. }


  20. });
  21. jQuery(document).ready(function(){
  22. if ($('.container:nth-of-type(2)').is(':visible')) {
  23. $('#main').css({'background' : 'red'});
  24. }
  25. else {}
  26. });


Here's a jsFiddle link