You are viewing an old revision of this post, from April 15, 2016 @ 15:54:08. See below for differences between this version and the current revision.

jQuery – Some good practices

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

Revision Differences

There are no differences between the April 15, 2016 @ 15:54:08 revision and the current revision. (Maybe only post meta information was changed.)

No comments yet.

Leave a Reply