javascript drag and drop list

;['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
  dropArea.addEventListener(eventName, preventDefaults, false)
})

function preventDefaults (e) {
  e.preventDefault()
  e.stopPropagation()
}

4
4
Knelis tonie 210 points

                                    // Code By Webdevtrick ( https://webdevtrick.com )
var btn = document.querySelector('.add');
var remove = document.querySelector('.draggable');
 
function dragStart(e) {
  this.style.opacity = '0.4';
  dragSrcEl = this;
  e.dataTransfer.effectAllowed = 'move';
  e.dataTransfer.setData('text/html', this.innerHTML);
};
 
function dragEnter(e) {
  this.classList.add('over');
}
 
function dragLeave(e) {
  e.stopPropagation();
  this.classList.remove('over');
}
 
function dragOver(e) {
  e.preventDefault();
  e.dataTransfer.dropEffect = 'move';
  return false;
}
 
function dragDrop(e) {
  if (dragSrcEl != this) {
    dragSrcEl.innerHTML = this.innerHTML;
    this.innerHTML = e.dataTransfer.getData('text/html');
  }
  return false;
}
 
function dragEnd(e) {
  var listItens = document.querySelectorAll('.draggable');
  [].forEach.call(listItens, function(item) {
    item.classList.remove('over');
  });
  this.style.opacity = '1';
}
 
function addEventsDragAndDrop(el) {
  el.addEventListener('dragstart', dragStart, false);
  el.addEventListener('dragenter', dragEnter, false);
  el.addEventListener('dragover', dragOver, false);
  el.addEventListener('dragleave', dragLeave, false);
  el.addEventListener('drop', dragDrop, false);
  el.addEventListener('dragend', dragEnd, false);
}
 
var listItens = document.querySelectorAll('.draggable');
[].forEach.call(listItens, function(item) {
  addEventsDragAndDrop(item);
});
 
function addNewItem() {
  var newItem = document.querySelector('.input').value;
  if (newItem != '') {
    document.querySelector('.input').value = '';
    var li = document.createElement('li');
    var attr = document.createAttribute('draggable');
    var ul = document.querySelector('ul');
    li.className = 'draggable';
    attr.value = 'true';
    li.setAttributeNode(attr);
    li.appendChild(document.createTextNode(newItem));
    ul.appendChild(li);
    addEventsDragAndDrop(li);
  }
}
 
btn.addEventListener('click', addNewItem);

4 (4 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
js drag and drop photo editor drag and drop image upload with js js no drag image how to show image in drag and drop in html javascript image drag code javascript drag and drop image inside div html drag image in image javascript drag drop image javascript drag and drop list order drag and drop elements in js how to drag and drop images javascript how to drag and drop javascript Drag and drop list items in JavaScript Drag and drop onto image html drag and drop image html code html drag and drop image drag and drop images javascript libraray javascript drag drop between 2 list vanilla javascript drag and drop list js drag and drop list mdn js drag and drop list target how to drag image in js drag and drop src of image injs drag and drop in html images js drag and drop list js draggable image drag and drop images from web js how to code drag images and drop in between images using javascript drag and drop list js drag and drop image in html and display drag image example drage and drop list using js drag drop list javascript drag and drop to image javascript drag and drop todo list javascript js drag and drop images preview js create a drag and drop input images image drag event js js drag drop image js drag drop png drag and drop using js drag and drop image on html drag and drop image on canvas html5 js drag and drop dropend Draggable list items JavaScript how to make a draggable list js vaniila js drag drop list drag image javascript image drag and drop javascript javascript to drag and drop image draggable list js drag and drop image example js drag and drop image example jd drag and dropt javascript how to make a draggable list javascript javascript drag and drop sortable list how to make image drag and drop in javascript sort list drag drop js drag and drop drop js image drag and drop js drag drop img set src Drag and drop image upload JavaScript js how to make a list items rearrange drop and drag javascript dragable list how to drag and drop images from the web js drag and drop images js drag and drop in js js drag drpp image event li drag and drop javascript javascript drag and drop reorder images javascript rearrange image drag drop javascript create a drag and drop list insert image drag and drop html html drag and drop list example drag drop javascript picture js drop and drag drag image in javascript drag image on javascript Drag and drop image JavaScript drag and drop example js drag and drop list items javascript js drag drop lib drag and drop image functionality with js html drag and drop img how to drag image in javascript javascript drag&drop image place drag ans drop image js drag and drop image link js ul draggable and drop drag and drop+js js draggable list html drag and drop list drag and drop with js drag and drop photo js drag adn drop lists js how to load image on drag and drop using js no drag image js js list with draggable items drag and drop image in html setting a drag image in javascript draggable image js javascript drag and dropd javascript drop and drag how to add drag and drop for a jpg in JS html image drag and drop javascript drag and drop load image javascript drag and drop image example drag and drop implementation in js mdb drag and drop menu javasciprt drag and drop image to html image drag and drop download javascript html5 drag and drop image drag and drop image in javascript how to get image by drag and drop with javascript javascript draggable list html drag and drop list items javascript drag and drop function drag and drop images javascript list drag and drop javascript drag and drop list javascript drag and drop list ordering javascript js set drag image drag and drop image js drag and drop sortable list javascript drag and drop in javascript js drag drop javascript drag drop image view javascript drag and drop image javascript drag and drop im draggable list javascript how to create drag and drop image in javascript in html set drag image html drag api of element JavaScript drag and drop example drag and drop javascript drag and drop li elements javascript drag and drop image upload js javascript no drag img javascript drag and drop lib drag and drop a sequence of images javascript d=code pen drag and drop a sequence of images javascript drag and drop to arrange images javascript js drag and drop js image drag and drop javascript drag and drop order list easy js drag and drop list drag and drop using javascript javascript drag and drop file drag and drop div using javascript drag and drop list in js how to set different style while drag in html css javascript javascript ondrop image javascript drag and drop drag and drop a text value drag and drop image drag and drop in a bar html drag image upload cursor drag drop image js drag and drop js example js drag and drop div html 5 draggable css drag and drop components drag and drop with html5 and js javascript drag example JavaScript drag and drop list example javascript drag and drop sortable drag drop javascript example js drag image and drop on image drag and drop in htm and get code html5 drag and drop list example drag and drop html css javascript draggble html exemple on drop function in javascript drag and drop code drop on drop off html css javascript swipe drag list items javascript swipe list item with drag javascfipt drag and drop list javascript drag and drop list html dragging droppable html dragon drop html drag and drop on element how to add drag option draggable tutorial drag and drop list with javascript drag drop html5 simple drag and drop
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source