You will need to use a couple of global variables to store the current element and the current color of the current element.
Then use a function to restore the elements (previous element) color when you select a new element.
Here is an example using some buttons.
When you click on a button the onclick event listener passes a reference to the button object and the new color to a function (change_color()) which first restores the previous buttons color, then stores the current button object and it's current color and then changes it's color.