javascript - Get an element of an array only once -
i have array 4 colors.
ex:
var c1 = getcolor(); var c2 = getcolor(); var c3 = getcolor(); var c4 = getcolor(); function geracolor() { var colors = ['#3498db', '#8e44ad', '#e67e22', '#1abc9c']; //return each value once }
i have list of teachers, each teacher has sublist disciplines.
this: professor , disciplines
each time click on professor, everytime click on professor's name, add disciplines inside calendar (alreay done).
what need is: need generate unique color (random or not) each professor click/expand.
obs: have limit of 4 professors, need generate 4 colors.
obs2: everytime click again on professor, closing disciplines. need make color using avaliable again.
afa understood question, maybe want achieve :
var colors = ["red", "green", "yellow", "orange", "blue", "pink"]; var defaultcolor = "black"; function getrndcolor() { var size = colors.length; var rnd = math.floor(math.random() * size); return colors.splice(rnd, 1)[0]; } function getcolor(elem) { var color = defaultcolor; if (elem.checked) { color = getrndcolor(); } else { colors.push(elem.parentelement.style.color); } elem.parentelement.style.color = color; console.log(colors); }
<span><input type="checkbox" onclick="getcolor(this)"/>get random color</span> <span><input type="checkbox" onclick="getcolor(this)"/>get random color</span> <br/> <span><input type="checkbox" onclick="getcolor(this)"/>get random color</span> <span><input type="checkbox" onclick="getcolor(this)"/>get random color</span> <br/>
Comments
Post a Comment