This is in reference of the question "reorder list elements - jQuery?" already asked here
Now I want to move a little further. What I am wanting to do, I am just wanting to build up a Comparison Product items for an ecommerce.
So, here I find the reordering a list item. But I want to add two button in each list item named "Left" and "Right", though here I have shown only one i.e "Right". On clicking the the "Right" button the "data-sortby" value will update to +1 and the list will move right i.e. in the next position and the next positioned list item will reorder to it's previous position. In short side by side movement of li items.
So the HTML is below:[just added the buttons]
<ul id="ulName">
<li data-sortby="1">three <a href="javascript:;" class="rGth" >Right</a></li>
<li data-sortby="2">one<a href="javascript:;" class="rGth" >Right</a> </li>
<li data-sortby="3">two<a href="javascript:;" class="rGth" >Right</a> </li>
<li data-sortby="4">four<a href="javascript:;" class="rGth" >Right</a> </li>
And the script is:
$(document).ready(function() {
var ul = $('ul#ulName'),
li = ul.children('li');
li.detach().sort(function(a,b) {
return $(a).data('sortby') - $(b).data('sortby');
});
ul.append(li);
$('.rGth').each(function(){
var thisli = $(this);
var parent = thisli.parent('li');
var right = parent.data('sortby') + 1;
var left = parent.data('sortby') - 1;
thisli.click(function(){
//alert(parent.data('sortby')+1);
alert(right);
alert(left);
parent.data('sortby', right);
parent.next().data('sortby',left);
});
});
});
But what I find that the "data-" attribute is not updating. Can anyone please help me what I am going wrong?
I have edited my code. It is giving an alert of the data attribute values but what I see it is not changing. I know I am doing a silly mess. Can anyone please help? Here is the Fiddle