Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think you need to also keep track of the order the elements were clicked. I would suggest you do this by keeping an object that will act as a sort of key/value pair, where the key is the "id" of the element clicking/unclicking, and the value is the number it was clicked (i.e. 1 = red, 2 = green, etc.). </p> <p>Then, in changeClass(), when adding the class, you can also add to the object the number it was clicked at. When removing the class, you can unset the key in the object and iterate over the items in the object and decrement anything that had a higher value. This decrement will allow you to adjust the color/number of anything that was clicked after that button that was just "unclicked" (e.g. a blue button should turn green if you "unclick" the green button).</p> <p>Might look something like this:</p> <p>Initialize with this:</p> <pre><code>var state = {}; </code></pre> <p>Do this when clicking (before incrementing nextclass):</p> <pre><code>state[element.id] = nextClass; </code></pre> <p>Do this when unclicking:</p> <pre><code>var removedNum = state[element.id]; delete state[element.id]; nextClass--; if (nextClass &lt; 0) { nextClass += classes.length; } var i; var keys = Object.keys(state); for (i = 0; i &lt; keys.length; i++) { if (state[keys[i]] &gt;= removedNum) { // decrement only things after the button unclicked var checkElement = document.getElementById(keys[i]); state[keys[i]]--; var checkClass = hasClass(checkElement, classes); if (checkClass) { // remove its existing class checkElement.classList.remove(checkClass); } // add the one it should now have checkElement.classList.add( classes[state[keys[i]]] ); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload