0

I've got an array of colors and would like to bind the css class when the user selects a certain color. I would like to change the color of the select input, depending on the chosen option.

<li v-for="product in products">
  <select v-model="product.color">
    <option v-for="color in colors" :class="{ color: isActive }">{{ color }}</option>
  </select>
</li>
colors: [
  {
    color: 'white',
    isActive: false
  },
  {
    color: 'black',
    isActive: false
  }
],
products: [
      {
        color: 'white'
      },
      {
        color: 'white'
      }
]

What is the best way to make it work?

1 Answer 1

1

I'm not sure if this is exactly what you're after, but this is how I would do it.

<li v-for="product in products">
  <select v-model="product.color">
    <option v-for="color in colors" :style="{ backgroundColor: color.color }">{{ color.color }}</option>
  </select>
</li>

See example here: https://jsfiddle.net/m4c00szq/

Updated Example

<li v-for="product in products">
  <select v-model="product.color" :style="{ backgroundColor: product.color }">
    <option v-for="color in colors" :style="{ backgroundColor: color.color }">{{ color.color }}</option>
  </select>
</li>

https://jsfiddle.net/m4c00szq/1/

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I appreciate your time! I would like to change the color of the select input, after choosing the certain option. So if you choose option 'white', then change the color of an input to 'white'.
Update example to show the select having a background color of selected color. Keep in mind you are limited to the colors the browser has defined in it's internal CSS unless you specify hexadecimal colors.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.