I am trying to copy the last class of each element to their parent li element in jQuery.
This is the current markup:
<nav class="menu-uber-main-menu-container">
<ul class="menu">
<li class="menu-item menu-item-type-post_type menu-item-object-page page_item">
<a href="#" class="">
<i class="fa fa-shopping-cart fa-lg menu-basket"></i>
<span>Home</span>
</a>
</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page page_item">
<a href="#" class="">
<i class="fa fa-line-chart fa-lg menu-chart"></i>
<span>About</span>
</a>
</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page page_item">
<a href="#" class="">
<i class="fa fa-truck fa-lg menu-truck"></i>
<span>Blog</span>
</a>
</li>
</ul>
</nav>
I want to copy and pass menu-basket, menu-chart, menu-truck to their parent li class="menu-item menu-item-type-post_type menu-item-object-page page_item"
The result should be:
<ul class="menu">
<li class="menu-item menu-item-type-post_type menu-item-object-page page_item menu-basket">
<a href="#" class="">
<i class="fa fa-shopping-cart fa-lg menu-basket"></i>
<span>Home</span>
</a>
</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page page_item menu-chart">
<a href="#" class="">
<i class="fa fa-line-chart fa-lg menu-chart"></i>
<span>About</span>
</a>
</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page page_item menu-truck">
<a href="#" class="">
<i class="fa fa-truck fa-lg menu-truck"></i>
<span>Blog</span>
</a>
</li>
</ul>
</nav>
The code for getting the last class of the element is this:
var lastClass = j('.menu-uber-main-menu-container > .menu > .menu-item > a > .fa').attr('class').split(' ').pop();
How to transfer each last class for each li element associatively?
.children()
or.parent()
with.addClass()
? – AdamMcquiffclass
attribute value as a string - which isn't a great idea. – Rory McCrossan