I have multiple boxes with a button. I want onclick select button variable increment. I don't want to be selected more than 6 so I have disabled other buttons.
If I remove any element. The removed one should continue on next click. Please see demo for better understanding.
var ct = ["1","2","3","4","5","6"];
var i = -1;
$('.goal1').each(function(index){
$(this).on('click', function(e){
i = i+1;
i = i % ct.length;
$(this).parents('li').addClass('selected');
$(this).fadeOut('50', function(){
$(this).parents('li').find('.goal2').fadeIn('50');
});
$(this).parents('li').find('.counter').text(ct[i]);
if($('li.selected').length > 5){
$('.goal1').prop('disabled', true);
}
e.preventDefault();
});
});
$('.goal2').each(function(index){
$(this).on('click', function(e){
i = i-1;
i = i % ct.length;
$(this).parents('li').removeClass('selected');
$(this).fadeOut('50',function(){
$(this).parents('li').find('.goal1').fadeIn('50');
});
$(this).parents('li').find('.counter').text('');
$('.goal1').prop('disabled', false);
e.preventDefault();
});
});
iyou should keep the state ofselectionslike:['1': true, '2': true, '3': false, '4': false]then when you click on un-selected box you assign this box a first number which hasfalsevalue (e.g usingdata-selection-number) and change the state ofselections. When you remove selection by clicking on selected box you again change a state ofselectionsand cleardata-selection-number.