How can jQuery be used to style a single selected option with a unique color?

I have a dropdown menu with a default placeholder ("ex. Crawler Excavator") displayed in lightgray. I want only this placeholder to be in lightgray when selected, while all other labels should be black. Currently, I am able to color the selected item lightgray, but it's affecting all other non-empty items as well. For clarification, please refer to the GIF and JSFiddle code provided below:

https://jsfiddle.net/fhgdrxz9/

Snippet Code CSS: css element.style {} select.form-control { text-overflow: visible; cursor: pointer; } .custom-border-radius>input, .custom-border-radius>select, .custom-border-radius div>span>span>.select2-selection .select2-selection--single { border-radius: 6px; border: 1px solid #DADADA; } .empty-label-gray>div>select, .empty-label-gray>select { color: #9e9e9e !important; } And here is an example HTML code block showing the dropdown list structure: html
If anyone has any suggestions on how to style only the selected empty caption and keep all other dropdown labels black, or if there is a way to remove the empty caption from the dropdown list altogether (e.g., "Crawler Excavator"), I would greatly appreciate it.

https://i.sstatic.net/0pNDz.gif

Answer №1

It may not be achievable solely with CSS, but here's a JavaScript demonstration. Check out this JSFiddle example for reference.

Javascript

const select = document.getElementById("32.MarketPlace.Service_PurchaseConfigurations.dropDown2.100_bom_555");

select.addEventListener("change", () => {
   if(select.value === ""){
     select.style.color = 'gray';
   } else {
     select.style.color = 'black';
   }
})

CSS

select {
  option {
    color: black;
  
    &:first-child {
      color: gray;
    }
  }
}

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

Utilizing interactions between a JavaScript scrollable container and Python/selenium

My goal is to utilize Selenium/Python in order to automate the process of downloading datasets from . While I am new to Javascript, I am determined to learn and overcome any obstacles that may arise during this project. Currently, I am focusing on the init ...

What causes the delay in monitoring the oplog in Meteor and Mongo?

I recently implemented Oplog tailing in my Meteor.js app using my MongoLab cluster to enhance performance, availability, and redundancy. However, I have noticed that since incorporating this feature, my publications are taking longer to complete. While a d ...

Tips on containing text within a div box without exceeding its boundaries

I've been struggling with containing the text in my display area. No matter what I try, the text keeps overflowing and it's driving me crazy. I've researched various solutions like using word-wrap: break-word;, adjusting width manually, but ...

Despite having magic_quotes_gpc turned off, jQuery Ajax still escapes strings

Even with magic_quotes_gpc = off on the server, the data I send via jQuery Ajax is still being escaped. When getting data directly from $_POST without using ajax, everything works fine and the data remains unescaped. However, when sending data through aja ...

What steps should I follow to enable my bot to generate or duplicate a new voice channel?

I needed my bot to either create a new voice server or clone an existing one. The variable "voic" contains the ID of the voice channel. voic.voiceChannel.clone(undefined, true, false, 'Needed a clone') // example from ...

Merging two functions for concealing an element

Below are three functions that control the behavior of hovering and clicking on different elements: $(".redDiv").hover(function() { $(".blueDiv").show(); }, function() { $(".blueDiv").hide(); }); $(".blueDiv").hover(function() { $(this).show(); ...

Adding a badge to a div in Angular 6: What you need to know!

How can I add a badge to a specific div in Angular 6? I have dynamic div elements in my HTML. I want to increase the counter for a specific div only, rather than increasing it for all divs at once. For example, I have five divs with IDs div1, div2, div3, ...

Revalidating doesn't seem to work as expected in Next JS

Reviewing my code on the development server function Home(props) { console.log("Hello") return ( <ul> {props.user.map((user) => ( <li key={user.id}>{user.email}</li> ))} </ul> ) } expo ...

Is it possible to attach an onclick event to modify a data point in Chart.js?

Currently using chartjs for a small project and facing some challenges with editing data points. Is there a built-in function in this library that allows me to bind an onclick event for displaying a pop-up and removing the point? This is what I am lookin ...

Step-by-step guide on developing an AngularJs provider using TypeScript

As I've developed a Directive that incorporates various Css classes, it would greatly enhance its flexibility if the Css classes could be configured at Application start within the config section. I believe utilizing a provider is the appropriate appr ...

Is there a way to resolve the scrolling problem on Google Maps?

When using the Google Maps V3 API, an issue arises where zooming out on the map using the scrollbar also causes the page to scroll along with it. This can be quite frustrating and so far I have not been able to find a way to disable this scrolling behavi ...

Implement Acrobat JavaScript to enforce a mandatory separate field when a checkbox is selected

As a designer with limited coding skills, I have developed an acrobat form that includes several due date fields. These fields are only mandatory if a specific checkbox is selected. I am looking for the javascript code that will validate the requirement: ...

Utilizing AngularJS for a Simple AJAX GET Request

I'm currently developing a Chrome extension with Angular JS. My goal is to retrieve and parse an HTML page from a third-party website. When using Angular JS $resource to achieve this (is there a more effective method?), I encountered an unusual objec ...

When Vue.js Vuex state changes, the template with v-if does not automatically refresh

I have tried setting the v-if value in computed, data, passing it as props, and directly referencing state, but it never seems to re-render despite the fact that the state I am checking for is changed to true. Currently, I am directly referencing the stor ...

JavaScript functioning properly in Google Chrome and Firefox but not in Internet Explorer 9

Welcome to the Lottery random generator tool! Feel free to use this in Google Chrome or Firefox, but please note that it may not work properly in Internet Explorer 9. If you encounter any issues while using this tool on IE9 and receive an error message st ...

What is the best way to limit the dimension of uploaded images to a specific height and width?

function validateImageDimensions(input) { if (input.files && input.files[0]) { var reader = new FileReader(); reader.onload = function(e) { $('#uploadForm + img').remove(); var img = $('<img> ...

Creating an extra dialogue box when a button is clicked in React

Having an issue with displaying a loading screen pop-up. I have an imported component named LoadingDialog that should render when the state property "loading" is true. When a user clicks a button on the current component, it triggers an API call that chang ...

Basic Jquery CSS style requests

Can you help me troubleshoot this issue? var hover = $('<img />').attr('src', hovers[i]).css('position', 'absolute') I'm having trouble getting th ...

Customize the appearance of a hyperlink

I am having difficulty styling a link's title using jQuery-mobile. Each link represents a player with unique abilities. When the user hovers over a player, I want a tooltip to display their special ability. Despite trying various code samples, none ha ...

What is the best way to position text both before and after the alert box is displayed?

Seeking guidance as a newbie in Javascript. I would like to allow the user to input their name into a text box and upon submitting it, display an alert saying "hello" followed by the user's name. The code currently outputs the user's name in an a ...