DEV Community

Cover image for Amazing Clear Input Using HTML & JS
Nikhil Bobade
Nikhil Bobade

Posted on

Amazing Clear Input Using HTML & JS

Hello

Today I created a Amazing Clear Input with HTML & JS. This is simple placeholder input I use JavaScript to clear input you can be also add a wipe animation to make more professional clear input. I hope you like this also comments about your thoughts.

For more content follow me on Instagram @developer_nikhil27.

Thank you!

Top comments (21)

Collapse
Β 
kylesureline profile image
Kyle Scheuerlein β€’

Did you know there is an <input type=β€œsearch” /> for stuff like this? I think it even has decent browser support.

Collapse
Β 
shagshag profile image
Georges Cubas β€’ β€’ Edited

Hello,

type="search" is for search, not for the ability to clean the input. Using it for an username, except if it's for search an username, is not a good practice. This could involve side effects in the future. Moreover Firefox Desktop doesn't seem to display the cross.

To OP, you have an error in your HTML : type=β€œsearch" the first quotes are invalid, curly quotes instead of straight quotes

Collapse
Β 
kylesureline profile image
Kyle Scheuerlein β€’

Good point! I think you’re right.

Collapse
Β 
nikhil27b profile image
Nikhil Bobade β€’

I actually first using simple type="text" but kyle suggest me to use type="search"

Collapse
Β 
nikhil27b profile image
Nikhil Bobade β€’

Yes I know about that but I forgot to use πŸ˜… and i use some of code from my previous post

Collapse
Β 
kylesureline profile image
Kyle Scheuerlein β€’

Well, as with everything... it has trade offs.

With your approach you get full control over the design of the β€œx”, but it requires JS and you need to use care to make sure it remains accessible.

With <input type=β€œsearch” /> you can’t change the look of the β€œx”, but it will match their OS, which could be desirable! It also won’t require JS to function and should be accessible.

Thread Thread
Β 
nikhil27b profile image
Nikhil Bobade β€’

I updated my code πŸ˜ƒ

Collapse
Β 
dillonheadley profile image
Dillon Headley β€’

If this was in a form element the x could actually be a button or input with type=β€œreset” which clears the inputs of a form natively and is accessible by default. No need to build that part yourself in js.

developer.mozilla.org/en-US/docs/W...

Collapse
Β 
nikhil27b profile image
Nikhil Bobade β€’

But reset is use for full form or input what if you only want to use for certain field

Collapse
Β 
dillonheadley profile image
Dillon Headley β€’

That’s a good point! In that case what you demonstrated would be the way to go πŸ˜„

Thread Thread
Β 
nikhil27b profile image
Nikhil Bobade β€’

There are many ways but I written a beginner friendly

Collapse
Β 
rajeshkumaryadavdotcom profile image
Rajesh Kumar Yadav β€’

img class could be more meaningful as that’s not an image so could be cross or icon or something more meaningful

JS need some optimisation as it’s targeting body, it should target form and some more JS logics can be improved

I know you are just sharing the idea, idea is good. Think of beginners who are just gonna copy paste your code.

Collapse
Β 
nikhil27b profile image
Nikhil Bobade β€’

Thanks I take care of that from next post

Collapse
Β 
iamcertainlyhappytoday profile image
james monteith β€’

But what does this search for? I would like to be able to search inside a page I had online of my own making so this sounds goods yet I do not no the parameters from this code of what or where is looks or reveals the answer.
Mention is made of beginners just doing a copy and paste but to me this is just a box that looks like it should do something but no explanation of what it does more than the very large promise of search de facto owt.

Collapse
Β 
nikhil27b profile image
Nikhil Bobade β€’

Hey I understand but now I explain the codes in my new post and this post is only have a clear functionality with HTML and javascript so if user want to clear the input so with that button user can clear the input

Collapse
Β 
amircahyadi profile image
Amir-cahyadi β€’

Nice ❀️

Collapse
Β 
nikhil27b profile image
Nikhil Bobade β€’

Thank you 😊

Collapse
Β 
amircahyadi profile image
Amir-cahyadi β€’

πŸ‘

Collapse
Β 
adam_cyclones profile image
Adam Crockett πŸŒ€ β€’

Here's another tip:

document.querySelector(".username").value = ''
Enter fullscreen mode Exit fullscreen mode

Nah just do this ☺️

document.querySelector(".username").clear();
Enter fullscreen mode Exit fullscreen mode
Collapse
Β 
adam_cyclones profile image
Adam Crockett πŸŒ€ β€’

Here's a nice tip, because .length 0 or -1 are falsey, you don't need to check if less than or equal to 0, just if truethy or falsely

Collapse
Β 
nikhil27b profile image
Nikhil Bobade β€’

Thank you 😊 Very nice tips