Sarit Sara
Sarit Sara

Reputation: 101

Is there any way to get the closest parent which have a specific class

In my application I have to get to the closest parents of the input/textarea which has the class of ('form-group').

I use the following code:

$("#contactus").validate({
    highlight: function (element, errorClass) {
        $(element).parent('.form-group').addClass('error-sign-up');
    }
});

For the cases of element which is input the code is fine (the direct parents of the inputs has the 'form-group'class.

The code above doesn't work with the following text area element:

<div class="form-group message">
    <label for="comment">Message*</label>
    <div class="flex-text-wrap">
        <pre>
            <span></span><br><br>
        </pre>
        <textarea class="pl-pr-10" name="message"></textarea>
    </div>
</div>

The error class has to be on the <div class="form-group message">

Upvotes: 0

Views: 1101

Answers (1)

Samuel O
Samuel O

Reputation: 2258

$("#contactus").validate({
    highlight: function (element) {
        $(element).closest('.form-group').addClass('error-sign-up');
    }
});

use closest instead of parrent jquery selector

Upvotes: 1

Related Questions