Modify the color of the dropdown list title within a designated area

I am looking to customize the font color of specific text within a dropdown list. The two options in the list are: 1. Doctor Name 1 (Discount 10%) and 2. Doctor Name 2 (Discount 15%). I would like to change the font color of only (Discount 10%) (to red) and (Discount 15%) (to blue). Can anyone guide me on how to achieve this customization?

<html>
<head>
</head>
<body>
<select name="">
    <option value="" >Select Doctor Name</option>
    <option value="1">Doctor Name 1 (Discount 10%)</option>
    <option value="2">Doctor Name 2 (Discount 15 %)</option>
</select>
</body>
<html>

Answer №1

For this particular problem, I would opt for using JavaScript as it provides better maintainability when dealing with multiple options and also allows for adjusting the color of the selected item, which cannot be achieved solely with CSS. Below is the approach I would take:

var colorSelect = document.getElementById('colorSelect');

// Initialize all colors to the value provided by data-color attribute or default to black
function initColorSelect() {
  var options = colorSelect.getElementsByTagName('option');
  var selIndex = colorSelect.selectedIndex;
  for (var i = 0; i < options.length; ++i) {
    var color = '#000';
    if (options[i].hasAttribute('data-color')) {
    color = options[i].dataset.color;
    }
    options[i].style.color = color;
    if (i == selIndex) colorSelect.style.color = color;
  }
}

// Change the color of the selected item
function colorSelectChanged() {
  var option = this.options[this.selectedIndex];
  var color = '#000';
  if (option.hasAttribute('data-color')) {
    color = option.dataset.color;
  }
  colorSelect.style.color = color;
}

window.addEventListener('load', initColorSelect, false);
colorSelect.addEventListener('change', colorSelectChanged, false);
<body>
  <select id="colorSelect">
    <option value="">Select Doctor Name</option>
    <option value="1" data-color="#f00">Doctor Name 1 (Discount 10%)</option>
    <option value="2" data-color="#00f">Doctor Name 2 (Discount 15 %)</option>
  </select>
</body>

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

Techniques for dynamically displaying or hiding elements depending on various input groups using jQuery?

I am faced with a challenging task of handling multiple product forms on a single page. Each form contains various color options represented by radio buttons, each marked with either an available or unavailable attribute. My goal is to display a customize ...

jQuery form validation with delay in error prompts

I am experiencing a strange issue with my HTML form validation function. It seems to be showing the alert div twice, and I can't figure out why this is happening. Adjusting the delay time seems to affect which field triggers the problem. Can anyone sp ...

I am facing difficulty creating a dropdown menu with nested options that only appear when the user hovers over the main selection

Here are the parent options I have and the code I have attempted to build. I would like to include sub-options such as phones, radios, speakers under the electronics parent option and many more. <select id="category" name="category" class="dropDown"& ...

The view function in Django will not be triggered by an Ajax call

Hey there, I'm currently facing an issue where my request to a Django view is not accessing the view function. Both the request and the view function are in the same app. I've tried debugging using tools, but it seems like no call is being receiv ...

Left-aligned and centered - Bootstrap

I need help figuring out how to center a list of items on the page, but have them aligned left. I am using bootstrap. I want the text to be in the center of the page but aligned left. <ul class='text-center'> <li class=& ...

My attempts to make a server-side function call with Ajax have been unsuccessful

I am trying to implement a simple ajax call as shown below: $.ajax({ url: "http://localhost:50488/siteadmin3/search.aspx/TestJquery", type: "post", data: { id: '100', Name: 'Nilesh' }, success:function(result){ ...

Unable to display the absolute path of the image src in WebStorm

Hey there! I recently started learning HTML5 and have been using WebStorm on my trusty Macbook Air for about two weeks now. I've encountered a problem with image sourcing that has left me puzzled. Here's my question: Why is it that images from ...

How can I define the boundaries for the scrolling of a static background image?

Is there a way to limit how far a fixed background image can scroll down a page? Essentially, is it possible to change the background attachment to static when the bottom of the background image is 10px away from the bottom edge of its div container? In t ...

CSS animation for typing effect not functioning correctly as text is being displayed before the animation starts

In the realm of HTML and CSS, I am currently immersed in a project that integrates a video with animated text. The text is brought to life within a comment box (class: "yellow_rec") that is nested inside an orange background box (class: "org_rec"). My main ...

Guidelines for showcasing a map on my website with the option for users to select a specific country

Is there a method to showcase a global map on my site and let the user select a country to receive relevant information? I am aware of embedding Google Maps, however, it includes unnecessary controls like zooming which I prefer not to inconvenience the us ...

Employing jQuery for adding a class to the initial TD in every table row

Currently, I am dealing with a table structured like this: <table width="100%" border="0" cellspacing="0" cellpadding="0" id="tablecontent"> <tr class="tablerow"> <td>This is the td I want to add a class to.</td> <td c ...

Creating new rows dynamically with jQuery

In my current setup, there is a table with one row and two columns - a textbox and a button: $(function() { var i = 1; $('#add').click(function() { i++; $('#dyn').append('<tr id="row' + i + '">&l ...

The children elements inside a parent div with Flexbox are now taking on the height of their container

I am facing an issue with my flexbox grid layout where one column contains a tall image while another column has two shorter images stacked on top of each other. The problem arises when viewing the layout in Internet Explorer, as the height of the smaller ...

I'm experiencing an issue where the changes I make in .aspx files are not showing up when I debug or rebuild

Recently, I have been experiencing an issue with my website where changes made to the .aspx file do not reflect when I debug or rebuild it. The only way for me to see the changes is by closing Visual Studio program and reopening the solution before debuggi ...

How can jQuery-based Ajax/PHP be leveraged for synchronization effects?

I am currently in the process of creating a groceries application that allows for synchronization with other family members who are part of a 'family' group. When one member updates the grocery list on their device, the changes should automatical ...

The jQuery window is not showing up as it was written in the code

This snippet of code is supposed to generate a dialog box, but instead, it only displays text with a CLOSE button. The code was sourced from the official Jquery website. <!doctype html> <html lang="en> <head> <meta charset="u ...

Establish the default bootstrap theme mode specifically for print formatting

Customizing the theme in Bootstrap is made easy by setting the data-bs-theme attribute on the body or parent element. While this feature is useful, we discovered that printing in light mode produces the best results. Instead of creating an entire set of st ...

Is there a way to extract all the aria-label attributes from elements that share a common class?

I am currently working on a program that tallies the number of reactions from Facebook posts, with each reaction stored in an aria label containing detailed information. I am encountering difficulty retrieving the values of these unique aria labels due to ...

Concealment Based on Surroundings

I'm currently working on implementing a new feature called 'context-based hiding' which involves hiding content based on mouse actions. I've been struggling to figure out how to create this feature and came across a website that has it ...

Guide to deactivating a control within a User Control

I anticipated that making this change would be straightforward, but I am encountering some difficulties. Initially, my asp.net markup for the server control appears like this: <ucTextArea:UserControlTextArea ID="taRemarks" runat="server" /> However ...