I have a list, which is sortable using jQuery UI sortable:
<ol id="sortable" class="ui-sortable">
<li style="margin-left:10px; width:100%;" class="block_anchor" data-id="498">Step 1</li>
<li style="margin-left:10px; width:100%;" class="block_anchor" data-id="499">Step 2</li>
<li style="margin-left:10px; width:100%;" class="block_anchor" data-id="500">Step 3</li>
<li style="margin-left:10px; width:100%;" class="block_anchor" data-id="501">Step 4</li>
</ol>
When the page loads, there aren't any <li>
items: I add them with jQuery by clicking on a button.
Right now, I can sort the list, but I'd also like to be able to change the position when I click on a <li></li>
element. I keep track of the elements position with their css attribute order
(the <ol>
elements is a flexbox so every item inside has an order). What I'm trying to do is to increment the order of an element of 1 when I click on it.
Here is what I did so far:
$('#sortable li').map(function(i, item){
var id = $(item).data('id');
$('li[data-id='+ id +']').on( "click", function() {
console.log("Clicked");
$('li[data-id='+ id +']').css('order', i + 1);
})
});
But even the console.log isn't appearing. Is it due to the fact that the <li>
items are added onClick of a button so they aren't here on page load when the function is executed ?
How should I proceed to go further ?
EDIT: this is not a duplicate, the topic mentionned was about reordering everything, which is not why I'm trying to do here