My question is how to crop / split list items by adding Class find from the list item text, like below?
My related question is here: Wrap List items by class name
Script Link
<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 the list items as follows:
<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>
<ul class="demo-text2">
<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 the jump list elements in two parts, for example, in the second HTML example. Add the class to the parent ul, replacing the text find from the example class="label"above (second HTML).
How can I do this simply using jQuery / JS? Here is what I tried:
$(".widget-content").each(function() {
var $li = $(this).find("li").unwrap();
var uniq = [];
$li.find("[class^=label]").attr("class", function(i, v) {
if (!~$.inArray(v, uniq)) uniq.push(v);
});
$.each(uniq, function(i, klas) {
$("a." + klas).closest("li").wrapAll($("<ul/>", {
class: klas
}));
});
});
Thanks in advance.
source
share