web-profile

jQuery .parent()

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

js:

$(function() {
    $('.target').parent().css('background-color', '#ffffaa'); // yellow
    $('.target').parent('.parent').css('background-color', '#aaffaa'); // green
});​

html:

<div class="well">
    should be yellow
    <div class="label target">.target</div>
</div>
<div class="well parent">
    should be green because of class="parent"
    <div class="label target">.target</div>
</div>
<div class="well">
    should be grey, because .parent() goes only one level up
    <div class="well">
        should be yellow
        <div class="label target">.target</div>
    </div>
</div>

Leave a Reply

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