I have some divs and by default only one is visible and all others are hidden. Every div has a different background color. Based on some buttons when are clicked , different divs are displayed.
I need to check which div is displayed and based on that I want to change the body background NOT on button click event.
I have some other buttons next/prev that move into divs, and if I change color on button click when I use those buttons the color stay the same for all divs!! For that i want to make an independent function that will check when divs are changed?
I have a jQuery code, which when a div i displayed the condition is true $('#divID').css('display') == 'block' is true the background color should be changed but it's not working!.
jsfiddle: DEMO My HTML code:
<body>
<br/><br/><br/>
<div id="ad1" style="display:block;" > Div content 1</div><br/>
<div id="ad2" style="display:none;" > Div content 2</div><br/>
<div id="ad3" style="display:none;" > Div content 3</div><br/>
<div id="ad4" style="display:none;" > Div content 4</div><br/>
<br/><br/><br/>
<button id='button1'> click 1</button>
<button id='button2'> click 2</button>
<button id='button3'> click 3</button>
<button id='button4'> click 4</button>
</body>
and jquery code:
$(document).ready(function () {
if($('#ad1').css('display') == 'block'){
$("body").css('background-color', '#ebb614');
}
if($('#ad2').css('display') == 'block'){
$("body").css('background-color', '#acb7a8');
}
if($('#ad3').css('display') == 'block'){
$("body").css('background-color', '#4f795b');
}
if($('#ad4').css('display') == 'block'){
$("body").css('background-color', '#7f7a6e');
}
$('#button1').on('click', function () {
$('#ad1').show();
$('#ad2 ,#ad3 , #ad4').hide();
});
$('#button2').on('click', function () {
$('#ad2').show();
$('#ad1 ,#ad3 , #ad4').hide();
});
$('#button3').on('click', function () {
$('#ad3').show();
$('#ad2 ,#ad1 , #ad4').hide();
});
$('#button4').on('click', function () {
$('#ad4').show();
$('#ad2 ,#ad3 , #ad1').hide();
});
});
$('#left-info1').is(':visible')is a more traditional way of determining if that element is visible