I am currently trying to figure out the following situation.
const invalidInput = document.querySelector('.ng-invalid:not(.no-check, .ng-invalid *)');
console.log(invalidInput.name);
<div class="ng-valid no-check">
<div class="ng-invalid">
<span>(</span>
<input class="ng-invalid" name="hello" id="hello">
<span>)</span>
<input class="ng-invalid" name="hello2" id="hello2">
</div>
</div>
<br>
<input class="ng-invalid" name="hello3" id="hello3">
the output should log hello3
here is the fiddle : fiddle
4
Answers
You can use
input.ng-invalid:not(div.ng-invalid .ng-invalid)
Where you are getting all
input.ng-invalid
that aren’t a child of a div with the class of.ng-invalid
.Assuming that your
input
is inside adiv
container, you can get your un-nested element by using direct child selector>
:I updated the script to include some extra details. First,
querySelectorAll()
to determine if the selector was pulling multiple items. Yes. Then I logged the tagname and full classlist.Your selector is essentially retrieving items with class
.ng-invalid
that are NOT nested within elements with that class and do not have.no-check
. This matches two items: the first div with ng-invalid and the final input. You are only returning one element, the first one, which is the div. I would suggest adding an "input" tag to your selector, like so.If you are only looking for
input
elements, then you can filter something like this: