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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

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.