0
This question will probably be answered in less than 5 minutes of so easy, but I’m cracking my head...
I’m going to simplify a lot here, but I think I can understand:
I create elements by javascript, each has its background color, are 8 colors that are in classes. (. back1, . back2...).
But there are only 8 colors, as I do to create more than 8 elements count back to 0?
NOTE: I know you can do this only in a simple way without using variables... But today is not my day here or at uncle google.
Sample code:
$(function(){
$("#adicionar").on("click",function(e){
$("#teste").append("<div class='fundo"+ ($("#teste div").length+1) +"'></div>");
e.preventDefault();
});
});
#teste div{
float:left;
clear: left;
width:40px;
height: 40px;
margin: 2px;
}
.fundo1{
background-color: #000;
}
.fundo2{
background-color: #00F;
}
.fundo3{
background-color: #0FF;
}
.fundo4{
background-color: #F00;
}
.fundo5{
background-color: #FF0;
}
.fundo6{
background-color: #FA0;
}
.fundo7{
background-color: #0F0;
}
.fundo8{
background-color: #F0F;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="teste">
</div>
<a href="#" id="adicionar">Adicionar</a>
Thank you, I thought of that possibility too. But as there are only 8 options has a lot of repetition and ends up getting a bit ugly the result for the user, so just exceed the standard of 8 same. (usually there will be no more than 6, but we need to prepare for that user who abuses the common sense hehe)
– Henrique Pauli