вот те рабочий пример, изучай:
<html>
<head>
<title>example</title>
<style>
.cl {
width: 100px;
height: 30px;
border: 1px solid silver;
}
.cl-one {
background-color: red;
font-weight: bold;
}
.cl-two {
background-color: black;
color: green;
border-radius: 5px;
font-style: italic;
}
</style>
</head>
<body>
<div class="cl" onclick="updateClass(this, 'cl-one');">123</div>
<div class="cl" onclick="updateClass(this, 'cl-two');">345</div>
<script>
function updateClass (ths, nameCl) {
var listCl = ths.getAttribute('class').split(' '),
index = listCl.indexOf(nameCl);
if (index == -1) {
listCl.push(nameCl);
} else {
listCl.splice(index, 1);
}
ths.setAttribute('class', listCl.join(' '));
}
</script>
</body>
</html>