Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

How to apply a background color to an <input> when it is filled

Hey everyone!

I really hope someone can stop me from pulling my hair off. I am trying to apply a background-color to an input field when it has something in it:

Say I have an input field for username like this:

<form>
  <label for="login-username">Username</label>
  <input type="text" id="login-username">
</form>

So say I have a default background of #fff, and whenever a user types anything on the input field it will turn yellow even though they will click on the next input field.

Some sort of a visual feedback that "this field is filled".

I already tried researching this topic but to no avail. I am asking you guys to help me out because I know for sure that this can be achieve with jQuery.

I'm sorry but my jQuery knowledge is still very primitive so I am asking help from you guys.

Thanks for dropping your answers! really appreciate any help!

1 Answer

Hey Edward, I remember someone asked something very similar some months ago. I created a codepen to illustrate a way we could achieve this. Here it is ! Hope It's going to help you in your project.

Hey Geoffrey!

Whohooh! Exactly what I needed man! Thank you so much! Finally found a way to pull it off :) Cheers!