Scott Scott - 1 year ago 50
Javascript Question

JQuery can you show a different element on each subsequent click?

I have a really simple idea... have a button (class="upload") that on each subsequent click, I want it to show a new div, for a set maximum number of times.

For example:

$('.upload').click(function() {
}, function() {
}, function() {
}, function() {

Obviously the code above does not work...

Is this even possible?

Answer Source

Declare two global variables, one for the total number of blocks to show, another for the current block that is shown.

When .upload is clicked on, check currentBlock < maxBlock. If it is, increment the current block that should be shown and concatenate .upload_block with currentBlock to get the jquery selector.

var maxBlock = 5;
var currentBlock = 1;
$('.upload').click(function() {
    if(currentBlock < maxBlock) {
        currentBlock ++;
        $('.upload_block'  + currentBlock).show(0);