My question is how to Wrap/Break list items by add Class find from list items text, example below?
My related question here: Wrap List elements by class name
<div class="widget-content">
<ul>
<li><span class="label"> demo-text1 </span> Test Content</li>
<li><span class="label"> demo-text2 </span> Test Content</li>
<li><span class="label"> demo-text1 </span> Test Content</li>
<li><span class="label"> demo-text2 </span> Test Content</li>
<li><span class="label"> demo-text1 </span> Test Content</li>
<li><span class="label"> demo-text2 </span> Test Content</li>
</ul>
</div>
I want to break list items like this:
<div class="widget-content">
<ul class="demo-text1">
<li><span class="label"> demo-text1 </span> Test Content</li>
<li><span class="label"> demo-text1 </span> Test Content</li>
<li><span class="label"> demo-text1 </span> Test Content</li>
</ul> <!-- Break part 1 -->
<ul class="demo-text2">
<!-- Break part 2 -->
<li><span class="label"> demo-text2 </span> Test Content</li>
<li><span class="label"> demo-text2 </span> Test Content</li>
<li><span class="label"> demo-text2 </span> Test Content</li>
</ul>
</div>
I mean break list items in two parts like second HTML example. Add class to both parent ul by replace text find from class="label"example given above (second HTML).
How can I do this simply by jQuery/JS ? Here is what I tried:
$(".widget-content").each(function() {
var $li = $(this).find("li").unwrap(); // unwrap removes the old UL wrapper
var uniq = [];
// Create a collection of unique "label*" classes:
$li.find("[class^=label]").attr("class", function(i, v) {
if (!~$.inArray(v, uniq)) uniq.push(v);
});
// Group LI by A class, and wrap into a new UL with the same class
$.each(uniq, function(i, klas) {
$("a." + klas).closest("li").wrapAll($("<ul/>", {
class: klas
}));
});
});
Thanks in advance.
lithere are, or do you want to split it in to multiple groups of 3? Also, please add the JS code you've written yourself to solve the issue.class="label", means if there are different text onclass="label"the please make new group. I mean split only when different text onclass="label", hope you understand :-)