I need to add a class to an element outside the ancestry. The following method does the task, however I'd like to lessen the use of parent()
instances.
The code
<h4 class="tab">Title</h4>
<div>
<div>
<div>
<div>
<ul>
<li><p class="send">add class to h4 from here</p></li>
</ul>
</div>
</div>
</div>
</div>
<script>
jQuery(function($) {
$(".send").parent().parent().parent().parent().parent().parent().prev().addClass("active");
});
</script>
I tried
All the following efforts fail to return the desired result.
$(".send").closest(".tab").addClass("active");
$(".send").find(".tab").addClass("active");
Update for clarity
The method is used for a slide down accordion where items are selected by the client, and each tab is highlighted if there are item(s) chosen. Also I have no control over the elements so I cannot add or remove classnames, and the structure is exactly as shown. I am just adding this method as a dynamic modification of the extension.
and inner