Для произвольного количества элементов
<style>
button {background: white}
</style>
<div id="myDiv">
<button>World</button>
<button>Wide</button>
<button>Web</button>
</div>
<script>
window.onload = function () {
var myDiv = document.getElementById('myDiv');
var firstColor = 'white';
var secondColor = 'red';
var cur = myDiv.children[0];
setBackground = function (element, color) {
element.style.background = color;
}
myDiv.onclick = function (e) {//onclick begin
e = e || event;
var target = e.target || e.srcElement;
if (target.parentNode == myDiv) {
if (target != cur) {
setBackground(target, secondColor);
setBackground (cur, firstColor);
cur = target;
cur.flag = 1;
} else if (target.flag != 1) {
setBackground(target, secondColor);
target.flag = 1;
} else {
setBackground(target, firstColor);
target.flag = 0;
}
}
}//onclick end
}
</script>