Skip to content Skip to sidebar Skip to footer

Change The Color Of An Option When Selected

I want an option to change color when selected by a user. For Example: a user selects the red option then a function would run that would change the color red. If the user then sel

Solution 1:

It looks like you haven't added an 'event' parameter to your function. Try this:

function changeColor(event) {
  var red = document.getElementById(red);
  var green = document.getElementById(green);
  var blue = document.getElementById(blue);

  if (event.target.value == red) {
    red.style.color = "red";
  } elseif (event.target.value == green) {
    green.style.color = "green";
  } elseif (event.target.value == blue) {
    blue.style.color = "blue";
  } else {
    alert("There was an error!");
  }};

Solution 2:

Try this. When you select an option you will recieve the <select> element in colorParam. If you select the first option, you will get Red in colorParam.value, but you are using IDs in lowerCase, so you can use toLowerCase() function to convert it. Then select the option element and apply styles.

functionchangeColor(colorParam) {
    let color = colorParam.value.toLowerCase();
    var optionElement = document.getElementById(color);
    optionElement.style.color = color;
};
<selectonchange="changeColor(this);"class="color"id="rgb"><optionid="red"value="Red">Red</option><optionid="green"value="Green">Green</option><optionid="blue"value="Blue">Blue</option><optionid="white"value="White">White</option><optionid="pink"value="Pink">Pink</option></select>

Solution 3:

I'm not sure you can style an <option> element. You can style the <select> element though. However, color doesn't seem to be a property you can change, background-color is.

If that's all you need you could as well inline the javascript code in the onchange handler attribute. You would need to set the values of the <option> elements to valid css colours.

<selectonchange="this.style.backgroundColor=this.value"><optionvalue="red">Red</option><optionvalue="green">Green</option><optionvalue="blue">Blue</option></select>

Solution 4:

Please check onchange only selected value color will get changed.

$(function() {
//The color of the selected value changedjQuery("#state-list").on("change", function() {

    if (jQuery(this).val()) {
    
  let colorselect= jQuery(this).find("option:selected").attr('id');
  jQuery('.statetext option').css("color", "#919ca1");
  jQuery('.statetext').find("option:selected").css("color",  colorselect);
  jQuery('.statetext').css("color",  colorselect)
  } else {
  jQuery('.statetext').css("color", "#919ca1");
  jQuery('.statetext option').css("color", "#919ca1");
  }
  });
});
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script><selectid="state-list"name="State"data-name="State"class="statetext"style="color: rgb(145, 156, 161);"><optionvalue="">State</option><optionvalue="CA"id="red">CA</option><optionvalue="BC"id="yellow">BC</option><optionvalue="DE"id="green">DE</option></select>

Post a Comment for "Change The Color Of An Option When Selected"