javascript - Change list elements index with jQuery -
i hava list:
<ul> <li class="item_1">element</li> <li class="item_2">element</li> <li class="item_3">element</li> </ul>
after drag , drop action i'm switching elements positions, after list looks this:
<ul> <li class="item_2">element</li> <li class="item_3">element</li> <li class="item_1">element</li> </ul>
i need replace classes of elements after dragging action sorted list again - state before dragging.
so, i'm doing this:
var setitemnumber = function(item) { item.each(function(i) { $(this).removeattr('class').attr('class', 'item_' + (i + 1)); }); }
what happening elements changing classes in same positions, looks doesn't change index , iteration acting on previous dom list state.
how can change this, or maybe else? can help?
i guess you looking this:
reindexitems = function() { var items = $('ul li'); var = 1; items.each(function() { $(this).removeattr('class').attr('class', 'item_' + i++); }); }
Comments
Post a Comment