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>

html:


<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 Comment