I've come across an issue with my HTML and JavaScript code for a select drop-down box. My goal is to have the background color of the selected option update to match the color selected by the user.
Currently, when using Firefox, the hover color changes to blue when you hover over an option and then the selected background color changes accordingly. However, when testing the same code in Chrome, it works as intended by using the selected option's background color.
How can I ensure cross-platform compatibility and make this work consistently on all browsers?
$("#color_me1").change(function(){
var color = $("option:selected", this).css("background-color");
$("#color_me1").css("background-color", color);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label>Color One:</label>
<select id="color_me1" name="color1">
<option disabled selected value> -- select color -- </option>
<option value="800000" style="background-color:#800000">Maroon</option>
<option value="FF0000" style="background-color:#FF0000">Red</option>
<option value="FFA500" style="background-color:#FFA500">Orange</option>
<option value="FFFF00" style="background-color:#FFFF00">Yellow</option>
<option value="808000" style="background-color:#808000">Olive</option>
<option value="008000" style="background-color:#008000">Green</option>
<option value="800080" style="background-color:#800080">Purple</option>
<option value="FF00FF" style="background-color:#FF00FF">Fuchsia</option>
<option value="00FF00" style="background-color:#00FF00">Lime</option>
<option value="008080" style="background-color:#008080">Teal</option>
<option value="00FFFF" style="background-color:#00FFFF">Aqua</option>
<option value="0000FF" style="background-color:#0000FF">Blue</option>
<option value="000080" style="background-color:#000080">Navy</option>
<option value="808080" style="background-color:#808080">Gray</option>
<option value="FFFFFF" style="background-color:#FFFFFF">White</option>
</select>
<br>