views:

53

answers:

1

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 is the label not fading out when I focus on the input? I know for sure that the focus event is happening.

Thanks

+5  A: 

Closest looks through the "parents" not siblings. What you want is 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 element if it meets the requirements."

Doug Neiner
+1 You tha man, Neiner :)
Jonathan Sampson
@Jonathan I knew you were on, so I had to type super fast and post often :)
Doug Neiner