dynamic dropdown displaying colors for selection

Using a bootstrap form to add a printer involves selecting the material and color. The dynamic dropdowns are functioning correctly, but it is necessary to display the selected color for user clarity. Please guide me on how to pass the color along with its respective name from the script so that users can easily see which color they are adding.

https://i.sstatic.net/Poa4P.png

 ...JavaScript code here... 
<?php
...PHP code here...
include '../header.php';
?>

<!DOCTYPE html>
<html>

...HTML content here...

Answer №1

To enhance the functionality of your addOption function, consider implementing a background color feature for each option or assigning them a specific class.

function addOption(selectbox, value, text) {
  var optn = document.createElement("OPTION");
  optn.text = text;
  optn.value = value;
  if(optn.value != ('Transparent/clear' || 'Glow-in-the-dark')){
    optn.style.background = value
  }
  selectbox.options.add(optn)
}

An alternative approach would be to achieve this using CSS styling:

option[value="Black"] {
  background: black;
}

option[value="Red"] {
  background: red;
}

/* Extend this list with more colors as needed */

Check out the demo below:

function addOption(selectbox, value, text) {
  var optn = document.createElement("OPTION");
  optn.text = text;
  optn.value = value;
  if(optn.value != ('Transparent/clear' || 'Glow-in-the-dark')) {
    optn.style.background = value;
  }
  selectbox.options.add(optn);
}

// Demo Implementation
const selectBox = document.querySelector('select')
const colors = ['Red', 'Transparent/clear', 'Glow-in-the-dark', 'Blue', 'Goldenrod']

colors.forEach(color => addOption(selectBox, color, color))
<select></select>

Answer №2

Here is a solution for your problem.

<select>
    <option value="1" style="background:red">Red</option>
    <option value="2" style="background:yellow">Yellow</option>
    <option value="3" style="background:purple">Purple</option>
</select>

To make changes, update the addOption() method as shown below. When using the function, specify the color.

function addOption(selectbox, value, text, color) // added an extra parameter - color.
{
    var optn = document.createElement("OPTION");
    optn.text = text;
    optn.value = value;
	
    optn.style.backgroundColor = color; // new statement
       
    selectbox.options.add(optn);
}

Use the function call like this

// set the color as blue when calling.
addOption(document.drop_list.SubCat1,"Blue", "Blue", "blue");

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

Determine the number of input tags within a div element by utilizing the closest property in jQuery

Sorry for the silly question, but I've been struggling to find a solution. Spent hours scratching my head. Here is my HTML structure: <div class= 'container'> <div class="someclass"> <input>some content</in ...

`Vanilla JavaScript AJAX request without using any external libraries or

Seeking advice on creating an ajax request function that is compatible across various browsers without relying on a javascript framework such as jQuery. Any ideas or suggestions are appreciated! ...

Handling node_redis callbacks in Node.js

Delving into node.js has been quite the adventure for me, though I admit that I may be missing a few key points along the way. My main goal is to query an hmap containing a list of rooms and then iterate through each room to gather more details such as the ...

An unexpected page transition occurs when attempting to delete a link

I've successfully created an HTML table that dynamically adds rows and provides an option to delete the current row. Each row represents data retrieved from MongoDB, and upon clicking the delete button, I aim to delete the corresponding item from the ...

Exploring how to browse and dynamically assign image sources with JavaScript/jQuery in an Asp.net mvc 4 framework

On my .cshtml page, I have an <img/> tag. <img id="ImageID" alt="" width="100%" height="100%" /> I want to dynamically set the src attribute by selecting the image file path using a file control. Although I attempted the following code in my ...

I successfully created a Chinese writing practice deck in Anki, which is functional on the desktop version but unfortunately not compatible with Ankidroid

While AnkiDesktop is functioning properly, there seems to be an issue with character encoding in Ankidroid. Despite trying numerous solutions, the problem persists. The character length displays correctly in Anki Desktop, but not in Ankidroid. For example ...

Examining for information using the fetch method

Utilizing the fetch method for API calls that provide JSON data. Sometimes, the response from the API call includes a status of OK but with a content value of null. Initially, I relied on solely checking the status for content which led to errors when the ...

Troubleshooting Problem with Card Layout in Basic Bootstrap Template

While using a basic Bootstrap template from Mobirise for my website, I ran into an issue. I am trying to create 5 Bootstrap cards with buttons in a row, instead of the maximum of 4 that is currently available. You can view an example of these cards here: ...

How to properly size a child div inside a parent container

I'm having trouble with sizing a child div inside a parent div. The problem is that the child div's size changes according to the number of elements it contains, but I want all the child divs to be the same size regardless. This issue arises with ...

The altered variable refuses to show up

Currently, I am working on a project to create a Skate Dice program that will select random numbers and display the results simultaneously. Unfortunately, I have encountered an issue where the randomly generated number is not being shown; instead, it dis ...

Building a dynamic AngularJS module on-the-fly: A step-by-step guide

I am trying to dynamically create AngularJS modules, and here is what I have so far: <section class="panel portlet-item" id="crashpanel"></section> <script type="text/javascript"> var angularApp = angular.module("Crashpanel", []); angula ...

What is causing my button to act in this way?

Initially, the website redirects to an undefined URL and then to test.com. I am looking for a way to implement a redirection sequence from to and finally to <head> <script type="text/javascript"> <!-- function popup(url ...

Using Vue js to scrollIntoView will only affect the specific element's div

I am currently working on a Vue page that consists of two divs. The Select-Div contains a list of elements, and when I click on an element from the list, the Scroll-Div is supposed to scroll to the corresponding ID element inside it. Ideally, the scrolling ...

There are certain lines of JavaScript/Node.js code that are failing to execute

app.get is not being executed. I have also attempted to include app.listen(3000). My goal is to retrieve the parameter passed from the first web page. This code is designed to fetch parameters sent by another web page and then construct a MySQL query and ...

The options in the drop-down menu appear to be stuck and remain in place

Can someone with expertise lend me a hand? I've been racking my brain over this issue, and while I feel like I'm on the right track, I just can't seem to make my sub-sub menu items drop down to the right on hover. I suspect there might be c ...

explore the route with the help of jquery scrolling feature

Is there a way to implement scrolling functionality for tab headers similar to this demo? I have a list of elements within a div that I need to scroll like the tabs in the demo. I attempted to achieve this by setting the position of the inner elements to ...

Utilizing a Material UI custom theme in React with Typescript: A step-by-step guide

Upon using the tool , I received a js file and a json file following the paths mentioned on the theme generator page: // src/ui/theme/index.js /* src/ui/theme/theme.json */ The files operate smoothly when left with the .js extension. However, when I attem ...

The width of the Ion-slide is dynamically determined by the styling

After transitioning my Ionic 2 project to Ionic 3, I encountered issues with ion-slides which are affecting my app's functionality. Upon app loading, specific widths are being defined in the style tags on the slides, disrupting the intended styling. ...

execute ajax within a standalone javascript function

I am just starting to learn about jquery and ajax, and I have a specific requirement to call ajax from a separate javascript function. The issue is that the JSP file is dynamically generated and the button IDs in the JSP file are also created using a for l ...

What is the technique for placing a div element directly within the href attribute in a shape

I am wondering if it is feasible to include the following divs <div id="cal1"> [dopbsp id="1" lang=it]</div> <div id="cal2"> [dopbsp id="1" lang=it]</div> <div id="cal3"> [dopbsp id="1" lang=it]</div> directly wit ...