My jQuery Script:
My jQuery script is quite a large file however I have trimmed it down to the most relevant parts for this question as seen below;
$(document).ready(function() {
"use strict";
$(document).on('click', function(e) {
if (($(e.target).attr('data-action')) || $(e.target).parent().data('action')) {
if ($(e.target).attr('data-action')) {
action = $(e.target).data('action');
} else {
action = $(e.target).parent().data('action');
}
switch (action) {
case 'mail-pin':
// Code for 'mail-pin' click
break;
default:
return;
}
}
});
});
My HTML Structure:
<!-- === INBOX LIST STARTS === -->
<div class="inbox-content clearfix">
<div class="Head">
<!-- Code for inbox header -->
</div>
<div class="Body clearfix">
<div class="Pinned">
<div class="Mail clearfix" data-mail-ID="1234">
<!-- Mail Item -->
</div>
<div class="Mail clearfix" data-mail-ID="1235">
<!-- Mail Item -->
</div>
</div>
<div class="Standard">
<div class="Mail clearfix" data-mail-ID="1233">
<!-- Mail Item -->
</div>
<div class="Mail clearfix" data-mail-ID="1236">
<!-- Mail Item -->
</div>
</div>
</div>
</div>
Question
First of all, I know how to check where the item is to be moved either from the .Pinned
element or the .Standard
to the other element via such means seen below;
case 'mail-pin':
console.log('Hello');
if ($(e.target).closest('.Mail').parent().hasClass('Pinned')) {
console.log('It is pinned.');
} else {
console.log('It is not pinned.');
}
break;
What I don't understand how to achieve is how to append the content in the correct location and by this I refer to the order taken from the data-mail-ID="1233"
so that upon clicking to either pin or unpin, the content will be appended to the place, so if you click to pin mail ID X it will append after X - 1 and visa versa if the item is unpinned.
Important Note:
Each list only displays 20 items per page and upon clicking to go to the next or previous page, the page fetches the content which would have been modified upon clicking to pin or unpin therefore this script would only be relevant for those ID's which can be found on that page, thus meaning if you unpin ID 123 and 122 cannot be found, it is simply removed and for pinning, the append part would only occur if .Pinned
is visible otherwise that item just removes.