Boxes Changes When a Character is clicked

How can I make the boxes change colour when a character is entererd? (e.g. When "/" is clicked, the box turns green?)
No description
14 Replies
Jochem
Jochem4mo ago
set an event listener on the input fields, either on the input event or change event, and apply a class based on the value of the input field
ἔρως
ἔρως4mo ago
wait, can you give more context on how you want it to work?
MarkBoots
MarkBoots4mo ago
is doable with only css
No description
ἔρως
ἔρως4mo ago
yeah, but is that how it is supposed to work? also, instead of valid, you should consider using user-valid as well
MarkBoots
MarkBoots4mo ago
yea okay
ἔρως
ἔρως4mo ago
and instead of :not(:placeholder-shown) you can use :user-invalid or :invalid too
MarkBoots
MarkBoots4mo ago
didn't think of that, true. but yea, it depends on what OP wants. it can be done with css, but maybe JS is easier when other things are needed aswell
ἔρως
ἔρως4mo ago
yeah, which is why i asked more context
Jochem
Jochem4mo ago
that also assumes that blank, | and / are the only valid values, once you hit a fourth, CSS isn't going to work anymore or rather, it's not going to work if there's more than white, orange, and green
ἔρως
ἔρως4mo ago
yup, which is why we need more context
MarkBoots
MarkBoots4mo ago
btw, was just checking the :user-valid. it doesnt update immediately on input, only on change (blur)
ἔρως
ἔρως4mo ago
yeah, but the :invalid does the :user-invalid differs from the :invalid in the sense that the user has to interact first for example, imagine a terms and conditions checkbox
MarkBoots
MarkBoots4mo ago
yea, i see the usecase
ἔρως
ἔρως4mo ago
with :invalid, it will show the invalid state as soon as you load the page with :user-invalid, it waits until the user tries to submit or unchecks it keep im mind that :user-valid and :user-invalid has some spotty support