I wondering how can I addClass
to the furthest parent from li
, I tried this and it working fine but I'm feel it's a bad logic, isn't it?
$('li').each(function() {
$(this).click(function() {
$(this).parent().parent().parent().parent().parent().parent().addClass('myClass');
});
});
.myClass {
background: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div> <!-- I want to add class to this parent -->
<div>
<div>
<div>
<div>
<ul>
<li><a>Click</a></li>
</ul>
</div>
</div>
</div>
</div>
</div>
I want to something like closest()
that work for children
, is there a way to access grand parent
? in this example I forced to write parent()
six times!