Joining categories - elements of a list

My goal is to dynamically bind a CSS class to an array of colors when a user selects a specific color. I want the select input's color to change based on the option chosen.

<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'
      }
]

I am seeking the most effective method to achieve this functionality. Any suggestions?

Answer №1

Not entirely sure if this is what you're looking for, but here's my approach.

<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>

Check out the example: https://jsfiddle.net/m4c00szq/

Revised 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>

Updated version available here

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Angular: the page continues to display outdated information after using router.navigate

My web app allows users to select products to purchase. They can pause the configuration process and resume it later. Currently, I am using localStorage to store information about the products and their prices. There is a page in my app that displays a ta ...

Options in the dropdown menu

After browsing online, I came across a functional example that I would like to replicate in a similar manner. Is there a way to show 2 different prices and currencies for the same selection in a drop-down menu without having separate drop-downs for each c ...

Tips for inserting a personalized image or icon into the ANTD Design menu

Can anyone help me figure out how to replace the default icons with a custom image or icon in this example: https://ant.design/components/layout/#components-layout-demo-side I attempted to do it by including the following code: <Menu.Item to="/" key=" ...

Differences Between jQuery Ajax POST Settings and GET Settings

I am in the process of developing a compact plug-in with a strong emphasis on code efficiency and stability. The issue is clearly outlined within the code snippet provided below (further explanation can be found underneath): $(document).ajaxSend(fun ...

Passing a leading zero function as an argument in JavaScript

Is there a method for JavaScript to interpret leading zeros as arguments (with the primitive value as number)? I currently have this code: let noLeadingZero = (number) => { return number } console.log('noLeadingZero(00):', noLeadin ...

AngularJs fails to render CSS styles

I'm encountering a small issue with my angularJS code. I am attempting to showcase JSON data that includes both CSS and HTML code. However, on my website, all that is displayed is a hard-coded form of HTML and CSS (resembling the code below). I have t ...

What is the reason behind Vuejs restricting the capability of a media query to toggle the visibility of a component?

Having the following code snippet in mycomponent.js: //mycompoent.js Vue.component('main-nav',{ template:` <div id="main-nav"> </div> ` }); Vue.component('menu-nav',{ template:` <div id="men ...

Guide on sending a PUT request using express.js

Currently delving into node.js and have a query regarding PUT requests I'm able to create an object and see it in the URL. However, I'm unsure of how to write a request to edit it - such as changing the price of a vehicle. Any guidance on writin ...

"Exploring the nuances of Knockout computed and subscriptions: a dive into timing complexities

Recently discovered an interesting behavior in KnockoutJS where subscription functions are evaluated before dependent computables. I'm looking for someone who can confirm this, as I haven't been able to find any information about the timing of Kn ...

I am looking to generate an array containing sub arrays so that I can easily iterate through the JSON data

I'm relatively new to creating subarrays and PHP, so please bear with me. I have some code that generates a JSON array, which is shown below. foreach ($result as $row) { $points = $row['points']; $price = ...

Encountering a runtime error while using a basic recursive search function in C++

Hey, I'm working on this program that involves searching through an Array of 100 integers to find a specific key. The linearSearch function should be recursive, so it calls itself. However, even though the program compiles fine, it only returns the fi ...

Having trouble retrieving information from the local API in React-Native

Currently, I have a web application built using React and an API developed in Laravel. Now, I am planning to create a mobile app that will also utilize the same API. However, I'm encountering an issue where I cannot fetch data due to receiving the err ...

Steps for altering the public folder in a Next.js project

In my monorepo, the structure is as follows: apps/ web/ - next.config.js - pages/ public/ The Next.js application is located in the apps/web/ directory. How can I modify the next.config.js file to instruct it to serve public assets from the root ...

"Launching a Vue.js SSR server in a Docker container: A step-by-step guide

I am working on a vuejs application with @akryum/ssr When I run npm run ssr:serve or npm run ssr:start everything functions properly. However, when attempting to utilize docker: version: '3.7' services: npm: build: context: ./ ...

Is there a way to extract and store the data from the nusoap response into a PHP variable or array

After successfully configuring nusoap to communicate with the chemspider server and receiving a response that can be displayed using print_r, I encountered an issue where using print only shows "Array." My query is straightforward - how do I convert this ...

Prevent postback in a LinkButton by disabling the default behavior when connecting it to a jQuery

I am struggling with preventing the page from posting back when clicking on a link button that I have bound to a click event using jQuery. Despite my attempts, the page always posts back, even though I have tried using event.preventDefault as suggested he ...

How to showcase the date in a unique format using Angular

Does anyone know of a JavaScript ES7 method that can convert the output of new Date() into the format shown below? If there isn't a built-in method, I am willing to manually parse or find/replace it myself. 2020-06-30 07.49.28 I would like the da ...

What techniques can I utilize to ensure Azure function generates JSON specifically for web browsers?

I have a functioning Azure function with the following code: module.exports = function(context, req) { // this is the complete source code, believe it or not context.done(null, {favoriteNumber: 3}); }; When I utilize a tool like Postman to access ...

Error: Unable to instantiate NGXLoggerHttpService due to an issue with the HttpBackend injection in the AppModule

I encountered an error message after updating my NgxLogger module: main.ts:17 NullInjectorError: StaticInjectorError(AppModule)[NGXLoggerHttpService -> HttpBackend]: StaticInjectorError(Platform: core)[NGXLoggerHttpService -> HttpBackend]: N ...

Encountering a reference error while attempting to troubleshoot streamline.js generated JavaScript code

After successfully setting up streamline.js and generating code using _node --standalone -c stest._js, I encountered an issue. The generated code was not readable and debugging it in tools like Chrome's developer console seemed impossible. However, I ...