I have a search text field like this one:
<div class="field">
<p class="control has-icons-left has-icons-right">
<input class="input is-normal is-rounded" type="text" placeholder="Search...">
<span class="icon is-small is-left">
<i class="fa-solid fa-filter"></i>
</span>
</p>
</div>
I want to add a clear button on it, that clears the text in the field if something is written on it. It would be nice if it appears only if there is some text in the field.
Thanks for the help.
2
Answers
You can add the bulma css class
is-hidden
to the icon, thenonInput
of the input element, remove that class to make it visible. Or, if the user removes all the input,add
the class back to hide it again.You may use the CSS pseudo-class
:placeholder-shown
to style the elements you want to hide based on the input condition.https://developer.mozilla.org/en-US/docs/Web/CSS/:placeholder-shown
So here in this demo I added some css rules that will hide the buttons when the placeholder is still being shown (so when the input is still not filled with text).
Here in this demo I put some efforts to style it the best way possible but there are few caveats. The main problem was using a
span
to model what should be actually abutton
instead for accessibility concerns!But I wanted to follow the guidelines shown on https://bulma.io/documentation/form/general/ that use that layout for icons instead. I tried with buttons but they looked very ugly. So please take this detail into account and spend yourself to address that issue.
Look here for further information about that topic: https://www.boia.org/blog/accessibility-tips-using-the-div-and-span-elements#:~:text=In%20general%2C%20you%20should%20avoid,with%20the%20HTML%20tabindex%20attribute.
side note: I understand that SO is not an helpdesk but yet sometimes I decide it’s worth spending time to see if I can solve something for my own purpose and it would be wasted if not shared