Shorten / improve CSS selector

Hi guys, I am wondering if there is a way to shorten that selector (used in a querySelector): :is(.ng-invalid, .ng-invalid:not(form) *):is(input, textarea)? What I want in the end is to select either input.ng-invalid or textarea.ng-invalid or .ng-invalid:not(form) input or .ng-invalid:not(form) textarea but I can't find a good way. I tried something like .ng-invalid:not(form):is(*, * *):is(input, textarea) where I hopped for :is(*, * *) to sort of "replace" the first * with .ng-invalid:not(form) but of course it's not working!
16 Replies
LcsGa
LcsGa3mo ago
Here is a reproduction of what I'm trying to achieve
ἔρως
ἔρως3mo ago
this sounds like something you can solve with css nesting oh, wait, ignore that which other elements receive the .ng-invalid class?
LcsGa
LcsGa3mo ago
Anything that is attached to angular form fields, no matter how the component is built 😬
ἔρως
ἔρως3mo ago
thats way too unspecific
LcsGa
LcsGa3mo ago
My selector already covers everything I need, I just to know if I can shorten it with a "trick" like I tried to do
ἔρως
ἔρως3mo ago
i knoe know
Shane
Shane3mo ago
gpt suggests
.ng-invalid :is(input, textarea), .ng-invalid:not(form) :is(input, textarea)
.ng-invalid :is(input, textarea), .ng-invalid:not(form) :is(input, textarea)
ἔρως
ἔρως3mo ago
that is different that matches only input and textarea inside .ng-invalid as long as the .ng-invalid isnt a form the original is completely different it matches any .ng-invalid that isn't a form but is an input or a select wait, no, the original matches any .ng-invalid and any .ng-invalid child that isn't a form or something like that, the match is different
Jochem
Jochem3mo ago
I appreciate you trying to help, but please don't just ask chatgpt. People come here to get answers from real humans, and everyone has access to gpt3.5 for free If they wanted an ai answer they could just get it themselves
MarkBoots
MarkBoots3mo ago
i tried different things, but the selector you have is already quite short for what you want and is still (kinda) readable/understandable. If there is a shorter way to do it, I suspect it won't be as clear If I may ask, what's the purpose of making it as short as possible?
ἔρως
ἔρως3mo ago
i have to agree, i cant find anything shorter and understandable and that does the same thing
MarkBoots
MarkBoots3mo ago
maybe this with :has? ...not sure
:has(>.ng-invalid:not(form)) :is(input,textarea)
:has(>.ng-invalid:not(form)) :is(input,textarea)
get the parent of a .ng-invalid that is not a form and get the input or textarea underneath probably wont work if the parent has multiple ng-invalids/inputs/textareas inside (you could wrap each one)
ἔρως
ἔρως3mo ago
that requires so much mental gymnastics because of the implied *
MarkBoots
MarkBoots3mo ago
Who would have thought I'd be a gymnast 😆
ἔρως
ἔρως3mo ago
you're more like an human compactor, and my brain is just mush
LcsGa
LcsGa3mo ago
Well it is mostly about curiosity! To see if we can do "better" with advanced/new features, that's it. If it is not readable, I won't adopt it BUT I would have learn something new Thanks, it looks very interesting, I'll try it tomorow!
Want results from more Discord servers?
Add your server
More Posts