DEV Community

Cover image for JavaScript Snippet to dynamically adjust text color of HTML Selects
Simon Köhler
Simon Köhler

Posted on

3 3

JavaScript Snippet to dynamically adjust text color of HTML Selects

If you use a HTML form with placeholder texts, you will notice that select dropdowns always have a darker font color than the placeholder texts of the input fields. With this simple JavaScript snippet you can dynamically color the select dropdowns on change so that they behave more like normal text fields.

What does this pure JavaScript Code do?

// Make sure your DOM is loaded
document.addEventListener("DOMContentLoaded",function(){

  // Find all select elements in the current DOM
  const selects = document.querySelectorAll('select');

  // Loop through all select elements
  selects.forEach((elem, i) => {
    // Add the class to make it look like a placeholder text
    elem.classList.add('text-muted');
    // Event listener to remove the class after selecting
    elem.addEventListener('change',function(e){
      // Remove the class to make it look normal (darker)
      elem.classList.remove('text-muted');
    });
  });

});
Enter fullscreen mode Exit fullscreen mode

Here's the working Example

Using the same Code without extra CSS

Of course you can use the same JavaScript Code without an extra CSS Class. Simply look at the next example:

// Make sure your DOM is loaded
document.addEventListener("DOMContentLoaded",function(){

  // Find all select elements in the current DOM
  const selects = document.querySelectorAll('select');

  // Loop through all select elements
  selects.forEach((elem, i) => {
    // Change the style attribute to lighten the text color
    elem.style.color = '#666';
    // Event listener to change the style of the element
    elem.addEventListener('change',function(e){
      // Change the style attribute to darken the text color
      elem.style.color = '#333';
    });
  });

});
Enter fullscreen mode Exit fullscreen mode

That was easy!

Do you like this little trick? Feel free to use it for your projects. Write me a comment if you have any suggestions for improvement or further ideas.

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

Neon image

Next.js applications: Set up a Neon project in seconds

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Get started →

👋 Kindness is contagious

Dive into this thoughtful article, cherished within the supportive DEV Community. Coders of every background are encouraged to share and grow our collective expertise.

A genuine "thank you" can brighten someone’s day—drop your appreciation in the comments below!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found value here? A quick thank you to the author makes a big difference.

Okay