I need to see if an input is in a class or not looking by the input
I want by this html code where the input is in a class
<div class="input-container">
<input type="text" name="realname">
</div>
the result true
And by this html code where the input is outside a class
<div class="input-container"></div>
<input type="hidden" name="id" value="1">
the result false
The input I will get with this javascript code
element.closest('form').querySelector('input[name=' + name + ']')
3
Answers
I think what you meant to say this instead:
…and that’s simple enough:
Element.matches
: https://developer.mozilla.org/en-US/docs/Web/API/Element/matchesCSS.escape
: https://developer.mozilla.org/en-US/docs/Web/API/CSS/escape_staticYou can make a simple check with
querySelector
:If you’re using
.closest
then why not: