There are several ways to achieve this with jQuery, mainly depending on how much you want to be βmagicalβ.
One pretty simple strategy would be to have a global variable called stepNumber and a data attribute at your steps, for example:
var stepNumber = 1; $(function(){ $('p[data-step-id="' + stepNumber + '"]').show(); $('#nextstep').click(function(){ $('p[data-step-id="' + stepNumber++ + '"]').fadeOut(); $('p[data-step-id="' + stepNumber + '"]').fadeIn(); }); });
.step { display: none; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div> <p id="text1" class="step" data-step-id="1">Step one goes here</p> <p id="text2" class="step" data-step-id="2">Step two goes here</p> <p id="text3" class="step" data-step-id="3">Step three goes here</p> <p id="nextstep"><a href="#">Next Step</a></p> <p id="nexttut"><a href="tutorial2.html">Next Tutorial</a></p> </div>
This approach is quite agnostic in the structure of your steps - they do not even have to be in the correct order. In this approach, step identifiers are not required.
but
We could simplify this by assuming that the steps are in order. We can use the nth-child CSS selector to find the next step, and we can compare this with the number of steps to see if the Next Tutorial button needs to be displayed again.
As in the first approach, step identifiers are not actually used.
var stepNumber = 1; $(function(){ $('p.step:nth-child(' + stepNumber + ')').show(); $('#nextstep').click(function(){ $('p.step:nth-child(' + stepNumber++ + ')').fadeOut(); $('p.step:nth-child(' + stepNumber + ')').fadeIn(); if(stepNumber == $('p.step').length) { $('#nextstep').hide(); $('#nexttut').show(); } }); });
.step, #nexttut { display: none; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div> <p id="text1" class="step">Step one goes here</p> <p id="text2" class="step">Step two goes here</p> <p id="text3" class="step">Step three goes here</p> <p id="nextstep"><a href="#">Next Step</a></p> <p id="nexttut"><a href="tutorial2.html">Next Tutorial</a></p> </div>
In both cases, I gave the steps a common CSS class so that they can be easily identified in Javascript.
source share