d.onclick = function click21() { if(event.ctrlKey) { d.style.background='#ccc'; } else { d.style.background='#6492cb';} } 

The color when you press CTRL + click changes to # ССС. How to make it so that when you press CTRL + click, the color changes to #CCC if you press CTRL + click again, then from #CCC to #DDD and so for example, 5 colors prepared in advance

    1 answer 1

    Add to code

     var colors = [ '#ccc', '#ddd', '#eee', '#fff', '#aaa' ]; var index = 0; function getNextColor() { if (index >= colors.length) { index = 0; } return colors[index++]; } 

    and replace

     d.style.background='#ccc'; 

    on

     d.style.background = getNextColor(); 

    jsFiddle

    • works only on 1 color, and the colors do not change further - Pavel
    • @Pavel added a link to jsFiddle in response, look, there is an example with the usual click - Stanislav Grotto
    • thanks a lot, it worked) - Pavel