I want to add <li>from one <ul>to another <ul>, created on the fly. I want to group list items into new sub-lists based on their attribute data-group.
<ul id="sortable1">
<li data-group="A">test</li>
<li data-group="A">test1</li>
<li data-group="B">test2</li>
<li data-group="B">test3</li>
<li data-group="C">test4</li>
</ul>
Basically, I try to skip this list and grab everything <li>from each group, and then move it to another <ul>.
This is what I have so far, but I am not getting the expected results. I have done this in Excel in the past, but cannot get it to work with jQuery.
var listItems = $("#sortable1").children("li");
listItems.each(function (idx, li) {
var product = $(li);
var str = $(this).text();
if (idx > 0) {
str += str;
if ($(this).data("group") != $(this).prev().data("group")) {
alert(str);
}
}
});
source
share