To style each option with a red background when selected, you'll need to use CSS. Unfortunately, the option elements in select cannot be directly styled through standard pseudo-classes like :selected or :focus. However, you can achieve your goal using JavaScript to apply a class to the select element.
Here's an approach using JavaScript and CSS:
-
CSS: Define a class that will style the selected option:
.selected-option {
background-color: red;
color: white;
}
-
JavaScript: Attach event listeners to the select element to dynamically apply the class to the selected options.
<script>
document.addEventListener("DOMContentLoaded", () => {
const selectElement = document.getElementById('bux-selection-dropdown__selection-dropdown-868066753');
selectElement.addEventListener('change', () => {
Array.from(selectElement.options).forEach(option => {
if (option.selected) {
option.classList.add('selected-option');
} else {
option.classList.remove('selected-option');
}
});
});
});
</script>
-
HTML: Your original HTML will remain the same for the most part, but here are all the sections combined:
<style>
.selected-option {
background-color: red;
color: white;
}
</style>
<select id="bux-selection-dropdown__selection-dropdown-868066753" data-drupal-selector="edit-field-available-on-target-id" multiple="multiple" name="field_available_on_target_id[]" size="9" class="form-select">
<option value="23">Main Campus</option>
<option value="27">Lima Campus</option>
<option value="28">Mansfield Campus</option>
<option value="25">Marion Campus</option>
<option value="26">Newark Campus</option>
<option value="29">Agricultural Technology Institute</option>
<option value="24">Wexner Medical Outposts</option>
<option value="30">CFAES County Outposts</option>
<option value="31">Stone Lab</option>
<option value="32">Central Ohio Technical College</option>
<option value="33">Marion Technical College</option>
</select>
<script>
document.addEventListener("DOMContentLoaded", () => {
const selectElement = document.getElementById('bux-selection-dropdown__selection-dropdown-868066753');
selectElement.addEventListener('change', () => {
Array.from(selectElement.options).forEach(option => {
if (option.selected) {
option.classList.add('selected-option');
} else {
option.classList.remove('selected-option');
}
});
});
});
</script>
The change event listener on the select element checks all the options each time the selection changes, adding the selected-option class to selected options and removing it from unselected ones.
Note: While this will visually change the background color of selected options in most browsers, please be aware that how this styling is rendered can vary between different browsers and operating systems.