Attention: Don't confuse jQuery Sortable with jQuery UI Sortable
I'm using jQuery Sortable to console.log() every <li> of every <ul> on the page, but when i rearrange a <li>, the console display the element twice
This is how the list look like on the page:
This is how the list look like on the console:
HTML code:
<div id="cmd_container">
<button class="w3-btn w3-blue" onclick="inclib()">Comando de linha única</button>
<button class="w3-btn w3-blue" onclick="conditional()">Comando de multiplas linhas</button>
<button class="w3-btn w3-light-grey" onclick="serialize()">Serializar lista</button>
</div>
<!-- The list -->
<p>Código padrão:</p>
<ul id="lista"></ul>
JS code:
$("#lista").sortable();
function serialize() {
console.clear();
$.each($("li"), function() {
console.log($(this).text());
});
}
// SINGLE LINE COMMAND (DEMO 1)
function inclib() {
var biblioteca = prompt("Digite o nome da biblioteca:");
if (biblioteca == "" || biblioteca == null) {
alert("Digite o nome do comando!");
} else {
var html = "" +
"<li>" + biblioteca + "</li>\n"
$("#lista").append(html);
}
}
// MULTIPLE LINES COMMAND (DEMO 2)
function conditional() {
var condicional = prompt("Digite a expressão conditional:");
if (condicional == "" || condicional == null) {
alert("Digite o nome do comando!");
} else {
var html = "" +
"<li>" +
condicional + " {\n" +
"<ul></ul>" +
"\n}" +
"</li>"
$("#lista").append(html);
}
}
PS: I've already included all the necessary libraries on the original code:
- jQuery 3.3.1
- jQuery Sortable


The problem is that you are trying to get all li items on the document, if you add two with
inclibwith text:Command 1andCommand 2then you will have the following html:The you add another using
conditionalwith text:Command 3then you will have the following html:If you move
Command 1orCommand 2insideCommand 3, then you will have the following html:When you select all the li items on your document, using
$("li"), you will get 3 items, but (in the case i described) the one with a nested ul will return all its content (including the content of the nested li) if you get its text using the jQuery.text()function, so you will get the following output:So, if you want to get a text representation of the sortable ul, you should only get the first level li items, using the following selector:
#lista > li.Check my working example. Hope it helps.
Edit According to the docs you should get a serialized version of the sortable using:
$("#lista").sortable("serialize")(i added an example to the snippet).