1. If you need to add class to the parent of an element has specific.
Example:
<ul class="parent_element">
<li class="normal_class">First</li>
<li class="normal_class child_element_specific_class">Second</li>
<li class="normal_class">Third</li>
</ul>
<ul class="parent_element">
<li class="normal_class">First</li>
<li class="normal_class">Second</li>
<li class="normal_class">Third</li>
</ul>
You can do this way to add class to the UL has child with class "child_element_specific_class"
jQuery(".parent_element:has(.child_element_specific_class)").addClass("wanted_element");
2. If you need to add class to the first specific element. You can do this way
Example:
<ul class="parent_element">
<li class="normal_class special_class">First</li>
<li class="normal_class what_we_want_here">Second</li>
<li class="normal_class special_class">Third</li>
</ul>
<ul class="parent_element">
<li class="normal_class special_class">First</li>
<li class="normal_class">Second</li>
<li class="normal_class">Third</li>
</ul>
You can do use this command to do what you want. jQuery('li.normal_class:not(".special_class"):first').addClass("wanted_element"); //Add class to the first specific element
Revisions
- April 15, 2016 @ 15:54:08 [Current Revision] by admin
- April 15, 2016 @ 15:54:08 by admin
No comments yet.