Нет кликаемого свойства. Таким образом, вам нужно будет прослушать прослушиватель события click и посмотреть, какой элемент был нажат.
function SizeButtonStyle (button) {
// remove the class of the previous selected element
var selected = document.querySelector('.size_button.selected')
if (selected) {
selected.classList.remove('selected')
}
button.classList.add('selected')
return false
}
.size_button {
display: inline-block;
background: #000;
border-radius: .2em;
font-family: "arial-black";
font-size: 1em;
min-width: 2em;
color: #FFF;
padding: .5em;
cursor: pointer;
text-align: center;
}
.size_button.selected {
background-color: #16a975;
color: #fbca01;
}
<button id="xs" onclick="return SizeButtonStyle(this)" class="size_button"><b>XS</b></button>
<button id="s" onclick="return SizeButtonStyle(this)" class="size_button"><b>S</b></button>
<button id="m" onclick="return SizeButtonStyle(this)" class="size_button"><b>M</b></button>
<button id="l" onclick="return SizeButtonStyle(this)" class="size_button"><b>L</b></button>
<button id="xl" onclick="return SizeButtonStyle(this)" class="size_button"><b>XL</b></button>
Как бы я это сделал со списком переключателей. JavaScript не требуется, и его просто обновить и поддерживать.
[name="size"] {
display: none;
}
[name="size"] + label {
display: inline-block;
background: #000;
border-radius: .2em;
font-family: "arial-black";
font-size: 1em;
min-width: 2em;
color: #FFF;
padding: .5em;
cursor: pointer;
text-align: center;
}
[name="size"]:checked + label {
background-color: #16a975;
color: #fbca01;
}
<input type="radio" id="xs" value="xs" name="size" /><label for="xs">XS</label>
<input type="radio" id="s" value="s" name="size" /><label for="s">S</label>
<input type="radio" id="m" value="m" name="size" /><label for="m">M</label>
<input type="radio" id="l" value="l" name="size" /><label for="l">L</label>
<input type="radio" id="xl" value="xl" name="size" /><label for="xl">XL</label>