I am facing an issue with multiple cards, each containing a Select element. When a user selects an option from the Select element, the card header changes color, but it also affects all other card headers. How can I separate them?
[data-background-color='empty'] {
background-color: #595959;
color: white;
}
[data-background-color='icu'] {
background-color: #9933ff;
color: white;
}
[data-background-color='med'] {
background-color: #0066ff;
color: white;
}
<div class="card">
<div class="card-header" data-background-color="empty">
<select class="form-select form-select-sm" aria-label=".form-select-sm example" id="unitSelect">
<option selected value="0">Empty</option>
<option value="1">ICU</option>
<option value="2">Med</option>
</select>
<!--some other content-->
</div>
</div>
$('#unitSelect').on('change', function() {
switch ($(this).val()) {
case '0':
$('.card-header').attr('data-background-color', 'empty');
break
case '1':
$('.card-header').attr('data-background-color', 'icu');
break;
case '2':
$('.card-header').attr('data-background-color', 'med');
break;
}
});