LATEST >>

Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » HTML-CSS-PHP-JavaScript / JavaScript Codes » Drag And Drop Reorder List & DIV Using Pure JavaScript

Drag And Drop Reorder List & DIV Using Pure JavaScript

Drag-And-Drop-Reorder-List-&-DIV-Using-Pure-JavaScript
This lightweight JavaScript code snippet helps you to create drag-and-drop reorder list functionality. It allows you to rearrange any elements within a parent node with a class name “drag-sort-enable” or anything with an event function handleDrag() and handleDrop(). This code snippet doesn’t support touch devices to sort lists with the drag-and-drop method. Anyhow, it is useful for desktop version webpages/apps to allow users to sort lists by the drag and drop method.

There are many code snippets available online or on many other blogs and websites, but everyone cannot optimize your blog or website, so you need some optimized code snippets. So now checkout out the code snippet for your blog and website that will give you all features for your desired code. Now grab the ready-to-use code and paste it where you want.

Features:

  1. Light Weight.
  2. Pure JavaScript.
  3. Cross Browser.
  4. No JQuery Files.
  5. Fully Customizable.
  6. Responsive.

How To Add Drag And Drop Reorder List & DIV Using Pure JavaScript?

There are a few easy and understandable steps to achieve your desired functionality that we are gonna share below. Follow each step perfectly.

Recommended For You:
15 Stylish And Attractive Custom CSS For Scroll Bar Using CSS3

CSS:

<style type="text/css">
ul {
margin: 0;
padding: 0;
}

li {
margin: 5px 0;
padding: 0 20px;
height: 40px;
line-height: 40px;
border-radius: 3px;
background: #e52d27; /* fallback for old browsers */
background: -webkit-linear-gradient(to right, #b31217, #e52d27); /* Chrome 10-25, Safari 5.1-6 */
background: linear-gradient(to right, #b31217, #e52d27); /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */

color: #fff;
list-style: none;
}

li.drag-sort-active {
background: transparent;
color: transparent;
border: 1px solid #4ca1af;
}

span.drag-sort-active {
background: transparent;
color: transparent;
}
</style>

HTML:

<ul class="drag-sort-enable">
<li>Application</li>
<li>Blank</li>
<li>Class</li>
<li>Data</li>
<li>Element</li>
</ul>

JavaScript:

<script type="text/javascript">
/* Made with love by @fitri
This is a component of my ReactJS project
https://www.codehim.com/vanilla-javascript/javascript-drag-and-drop-reorder-list */

function enableDragSort(listClass) {
const sortableLists = document.getElementsByClassName(listClass);
Array.prototype.map.call(sortableLists, (list) => {enableDragList(list)});
}

function enableDragList(list) {
Array.prototype.map.call(list.children, (item) => {enableDragItem(item)});
}

function enableDragItem(item) {
item.setAttribute('draggable', true)
item.ondrag = handleDrag;
item.ondragend = handleDrop;
}

function handleDrag(item) {
const selectedItem = item.target,
list = selectedItem.parentNode,
x = event.clientX,
y = event.clientY;

selectedItem.classList.add('drag-sort-active');
let swapItem = document.elementFromPoint(x, y) === null ? selectedItem : document.elementFromPoint(x, y);

if (list === swapItem.parentNode) {
swapItem = swapItem !== selectedItem.nextSibling ? swapItem : swapItem.nextSibling;
list.insertBefore(selectedItem, swapItem);
}
}

function handleDrop(item) {
item.target.classList.remove('drag-sort-active');
}

(()=> {enableDragSort('drag-sort-enable')})();
</script>

Customization:

No need to customize it. Just copy-paste. Rest edit the code as per comments and need.

Troubleshooting the Errors:

Do it with concentration and patience. Check your all steps again and all codes or scripts. If you find any error you can contact us anytime via comment or better via email, We are always here to help you.

Final Words:

That’s all we have. We hope that you liked this article. If you have any problem with this code in your template then feel free to contact us with a full explanation of your problem. We will reply to you as time allows us If you have any doubts or problems please comment below. We are happy to help you! If you liked this article, Don’t forget to share this with your friends so they can also take benefit from it and leave.

Recommended For You:
Where To Link/Embed JavaScript File/Code In Blogger And Website?

You Like It, Please Share This Recipe With Your Friends Using...

Be the first to write a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *