jQuery .closest()

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

js:


$('.target').closest('.parent').css('background-color', '#ffa');​

html:


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