Is it possible to use toggleclass on a button when a maximum amount of appended items is reached?
I want 6 appended divs/items to be the maximum. I've added a variable counter. So it increases when an item is appended and it decreases when the removebutton is clicked. The last step is to toggleclass the button (like a disabled button). So when the maximum amount is reached, the button toggleClass = red.
What I want:
On the sixth appended item, the button background changes to red and doesn't append anymore items (disabled). When the user removes one, the button toggles back to it's original state and the button fire the append event again. How can I integrate toggleClass in my code?
HTML
<div id="addbutton">ADD</div>
<div id="box"></div>
JS
var amount = 0;
var div = '<div>' +
'<input type="text"/>' +
'<input type="button" class="removebtn" value="delete"/>' +
'</div>';
$('#addbutton').click(function () {
amount++;
if (amount < 6) {
$('#box').append(div);
//alert(amount);
} else {
$(this).toggleClass(".red");
}
});
$('.removebtn').live('click', function () {
$(this).parent().remove();
amount--;
});