jQuery .parents()

jQuery .parents() gets all parent ancestors of the target element;
If you need to get parent element one level up check .parent() method;

<script>
$(function() {
    $('.target').parents().prepend($('<span class="label">parent</span> '));
    $('.target').parents('.selector').prepend($('<span class="label label-info">parent with selector</span> '));
});</script>
<div class="well">
    <div class="well selector">
        .selector
        <div class="well">
            <div class="well selector">
                .selector
                <div class="well">
                    <strong class="target">.target</strong>
                </div>
            </div>
        </div>
    </div>
</div>

Leave a Reply

Your email address will not be published. Required fields are marked *