Select a style option from the drop-down menu to be disabled once it is checked

Looking at the image below, I am seeking for Exp. Year (the disabled option) to appear grey as a placeholder when the page loads, and then turn black when an option like 2016 is clicked on. Is there a way to achieve this without using JavaScript?

JSFiddle

Current Behavior:

Desired Outcome: (Exp. Month appears grey initially, but switches to black when 2016 is selected)

.select-box {
  border: 1px solid $ghBlack;
  height: 36px;
  background: transparent;
  margin: 10px 0 14px 0;
  color: #000;
}

option:disabled {
  color: #a9a9a9;
}

option:not(:checked) {
  color: #a9a9a9;
}

Answer №1

To achieve this task, one approach you can take is shown below:

// Using an anonymous function as the change-event handler:
$('select').change(function () {
    // Modifying the 'color' property of the select element:
    $(this).css('color', function () {
        // Efficiently caching the 'this' variable for repeated use:
        var self = this,
        // Locating the options of the select element:
            opts = self.options;
        // Obtaining the currently selected option, and then checking if
        // it's the default-selected option (returns a Boolean); if it is,
        // we set the color to '#aaa', otherwise set it to '#000':
        return opts[self.selectedIndex].defaultSelected ? '#aaa' : '#000';
    });
// Triggering the change-event so that this executes on page-load:
}).change();

Check out the JS Fiddle demo here.

For more information, please refer to:

Answer №2

It seems that without the use of JavaScript, you won't be able to achieve your desired outcome. This is because the color of the main option is specifically defined by the CSS selector shown below:

.select-box {
  color: grey;
}

You do have the option to change the colors of the dropdown options when the select box is opened. For a demonstration, you can refer to this fiddle: http://jsfiddle.net/san6q621/

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

``I just retrieved data from two separate Mongoose documents in the database

I'm currently working on my first app using mongoose database. I'm facing an issue with writing an express API call that should return an object containing data from two documents. Every time I access the /data route, I receive an empty array as ...

Align an element to the center and then align a second element to the right using Tailwind CSS

Here is a visual representation of my goal: the dashed line indicates the center of the container. My objective is to horizontally center one div element and then right-align a second div within the same row, all while keeping both elements centered. htt ...

mobile input sliders for selecting ranges

While working on a custom audio player, I encountered an issue with the default html5 input range object. Everything else seems to be working perfectly, with all events firing as needed, except for a frustrating problem on mobile Safari with iOS 11.4: Whe ...

Build a custom form for the purpose of exporting an HTML file

I am in search of a way to provide my users with various "feature" choices that will assist them in creating an HTML file for an email. I already have the code ready for each feature, but I want users to be able to select a feature (such as Hero Image Head ...

Leveraging the while/for loops for the purpose of reading exactly two lines of input

I'm facing a challenge that involves handling two lines of input data. Input : 4 6 | 6 9 Output : 2 | 15 (the "|" signifies a line break) However, I am unsure about the best approach to interpret these two lines effectively. I attempted using a f ...

Rendering implemented in an Angular component through Three.js

Currently immersed in developing a dynamically generated three.js component within Angular. The statically created Plot3dComponent (via selector) functions flawlessly. However, encountering difficulties in rendering the component dynamically using Componen ...

What could be the reason for my CSS animation with :hover @keyframes not functioning?

As a newcomer, I am struggling to understand why this code isn't functioning correctly. My goal is to create the illusion of a flying bird. This is my HTML: <img src="http://dl.dropboxusercontent.com/u/105046436/tw.png" /> <br> <div c ...

Running the command "nexrjs create next-app" successfully generates all the necessary directories for the

After trying to install NextJS using both methods npm install next react react-dom and npx create-next-app appname, I noticed that my project directories are different from what they are supposed to look like: Instead of having pages, api(_app.js, index.j ...

Challenges with creating an increment and decrement form component using Vue

I've been working on this for almost three days now. My goal is to create a reusable component in Vue that allows a passed value to be incremented and decremented with the click of a button, and then submitted in a form. Below is my code: Parent Com ...

Anticipated outcome for absent callbacks in module API implementation

I am seeking advice on the expected behavior when developing a Node module API. It is becoming complicated in my module implementation to check if the caller has provided a callback before calling it. I am starting to believe that it may be the user's ...

Is it possible to create a website with a single session for all users using JavaScript

Greetings everyone, I am excited to be posting for the first time on stackoverflow. After dedicating a week to learning javascript and achieving some things I am proud of, I have hit a roadblock. As a self-learner, I kindly ask for your understanding. Cur ...

What is the best way to iterate through nested Models in CanJS?

I need to iterate through nested JSON data in a Model and pass it to my view file using CanJs. Any suggestions on how to effectively loop through this nested data in the model? ...

"Displaying 'Object Object' as a title when hovering over an element

I am currently using an accordion element to display several FAQs on a webpage. The code snippet for the accordion with content is as follows: { Object.keys(FAQS).map((key, index) => { return ( <div key={key}> ...

What is the best way to adjust the sprite's position in real-time

Look at this cool sprite I discovered! .common-spinner.common-spinner-40x55 { height: 55px; width: 40px; } .common-spinner { background: url("images/loading-sprite.png") no-repeat scroll 100% 100% transparent; } <div class="loading"> ...

Removing a class with JQuery using the Contains method

I am attempting to remove the "hasChild" class from the following element, <li class="theme-nav-item selected enabled hasChild" data-nav-url="http://www.removed.com/page/nav/3353642"></li> by specifically targeting the value 3353642 with the ...

Switching the div to display or hide

Within this block of code, I have successfully implemented functionality to hide and show two separate div elements. However, I am facing a minor issue where the display value of #MSOZoneCell_WebPartWPQ2 needs to be returned to 'table', as it is ...

Enhance your data visualization with d3.js version 7 by using scaleOrdinal to effortlessly color child nodes in

Previously, I utilized the following functions in d3 v3.5 to color the child nodes the same as the parent using scaleOrdinal(). However, this functionality seems to be ineffective in d3 v7. const colorScale = d3.scaleOrdinal() .domain( [ "Parent" ...

The fade in and fade out effects using JQuery are not functioning as expected despite adding a delay

I am attempting to achieve a text effect where the text fades in, stays visible for a moment, and then fades out. While I understand this can be done with CSS Keyframes, I am unsure of how to implement it with multiple animations. As a result, I am experi ...

The fusion of PHP and HTML coding

I have been attempting to integrate some PHP code into a combination of HTML and I am facing some challenges. Below is the code I am working with: <?php $result = mysqli_query($db,"SELECT * FROM classes"); while($record = mysqli_fetch_array($result)) ...

The OnClick function seems to be unresponsive, however, there are no error messages displaying in

Currently delving into React to enhance my skills, I decided to craft a joke generator. After establishing a local file and successfully fetching the data (jokes) to showcase on the browser, my current goal is to implement a button that, when clicked, will ...