-4

I want to toggle between 3 divs. Unfortunately it only works for 2 divs.

My code (for two divs):

$(function() {
  $('#playerbargreen').hide();
  $('#playerbaroff,#playerbargreen').click(function() {
    $('#playerbargreen,#playerbaroff').toggle();
  });
});

The end result must be:

show first div --> click --> hide first div and show second div --> Click --> hide second div and show third div

Has someone any idea? Thanks in advance!

freginold
  • 3,946
  • 3
  • 13
  • 28
user3130478
  • 207
  • 1
  • 4
  • 11

2 Answers2

0

Start with Div 2 & Div 3 hidden.

Create an on click event for Div 1 which hides Div 1 and shows Div 2.

Repeat for Div 2 to Div 3.

Something simple like the following should suffice:

$('#Div1').click(function() {
    $('#Div1').hide();
    $('#Div2').show();
});

$('#Div2').click(function() {
    $('#Div2').hide();
    $('#Div3').show();
});
melkisadek
  • 1,043
  • 1
  • 14
  • 33
0

You can try something like this

$('#Div2').hide();
$('#Div3').hide();

$('#Div1').click(function() {
    $('#Div1').hide();
    $('#Div2').show();
});

$('#Div2').click(function() {
    $('#Div2').hide();
    $('#Div3').show();
});
div{
width:100px;
height:100px;
background-color:green;
color:white;
text-align:center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="Div1">div1</div>
<div id="Div2">div2</div>
<div id="Div3">div3</div>
Sanchit Patiyal
  • 4,910
  • 1
  • 14
  • 31