jQuery .closest()

jQuery .closest() gets the first element that matches the selector, beginning at the current element and progressing up through the DOM tree;

<script>
$('.target').closest('.parent').css('background-color', '#ffa');</script>
<div class="well">
    <div class="well parent">.parent
        <div class="well">
            <div class="well">
                <span class="target">.target</span>
            </div>
        </div>
    </div>
</div>

Leave a Reply

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