Jump to content
TonyR

Clear button in input field to appear only if there is text

Recommended Posts

Hi

I have managed to get a clear button working in an input field but I do not know how to get to appear only when there is text in the field.

You can see what I have at the moment here: https://codepen.io/TonyRuttle/pen/EJNyVN

I am not sure if this is a CSS issue but a JS topic but I have added this topic in the CSS category.

Tony

Share this post


Link to post
Share on other sites

If you keep the input as required then you can use the :valid/invalid CSS selectors.

input:invalid + #clear { visibility: hidden; }

Separately, you shouldn't make the clear button use an ID because then you can't have more than one on a page. At least 90% of the time, if you're thinking of using an ID you should probably use a class instead.

  • Great Answer 1

Share this post


Link to post
Share on other sites
Posted (edited)

Thank you requinix. This is working perfectly.

Duly noted regarding the id/class. I have edited the above link to add your changes.

Edited by TonyR

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.