Jquery closest () doesn't work for me (or i don't work for it)

Given this jQuery:

$('div.MvcFieldWrapper :input').focus(function() {
      $(this).closest('label.MvcDynamicFieldError').fadeOut();
});

And given this HTML:

<div class="MvcFieldWrapper">
    <label class="MvcDynamicFieldPrompt">Enter your email address:</label>
    <label class="MvcDynamicFieldError">Required</label>
    <input type="text" value="" />
</div>

Why doesn't the shortcut disappear when I focus on input? I know for sure that a focal event is occurring.

thank

+3
source share
1 answer

The closest views of the “parents” are not siblings. Do you want prevAll:

$('div.MvcFieldWrapper :input').focus(function() {
      $(this).prevAll('label.MvcDynamicFieldError').fadeOut();
});

closest actually means "find the closest ancestor that matches the selector, including the already selected item if it meets the requirements."

+12
source

Source: https://habr.com/ru/post/1730728/


All Articles