I am following the excellent example I found here: CSS - How to Style a Selected Radio Buttons Label? trying to style a group of radio-buttons which I create dynamically using javascript. While I manage to create the buttons, I can't find a solution on how to change the background of the label of the radio button that is currently selected. In my example the color only changes during hovering but flips back to normal besides being selected.
My code on JSFiddle: https://jsfiddle.net/dsarh65p/2/
For reference:
HTML
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
<div class="btn-group" data-toggle="buttons">
<span id=car></span>
</div>
</body>
</html>
JS
var array = ["Saab", "Volvo", "BMW"];
var item = document.createElement('div');
item.className = 'radio-toolbar';
for (var i = 0; i < array.length; i++) {
var input = document.createElement('input');
var input_label = document.createElement('label');
input.type = 'radio';
input.name = 'radio-btn';
input.id = 'radio' + i;
input.value = "true";
input_label.innerHTML = array[i];
input_label.setAttribute("class", "btn btn-primary");
input_label.appendChild(input);
item.appendChild(input_label);
document.getElementById("car").appendChild(item);
}
CSS
.radio-toolbar input[type="radio"] {
display: none;
}
.radio-toolbar label {
display: inline-block;
background-color: #ddd;
padding: 4px 11px;
font-family: Arial;
font-size: 16px;
cursor: pointer;
}
.radio-toolbar input[type="radio"]:checked {
background-color: #bbb;
}