Edit: I know this looks like a duplicate question, but it's very specific to the HTML5 drag events. I'm not having a problem adding in jQuery events, just the HTML5 drag and drop functionality.
I've used basic jQuery several times to add drag-and-drop functionality to a page, but I'd like to use the new HTML5 drag-and-drop feature for my current project. Drag works great for any element I include in the HTML, but I don't seem to be able to add elements dynamically and have them be draggable. I suspect this is because all the draggable event listeners are bound when the page is loaded, before my new elements are created. Has anyone had any luck using HTML5 to make dynamically added elements draggable? Or is there a way to re-bind event listeners without reloading the page?
Using .bind()
with any of the HTML5 drag events doesn't seem to work. i.e,
$(newElement).bind('drag', function(e){
console.log('do you even drag, bro?');
});
won't ever fire, though .bind()
with a click event works fine. I'm guessing this is because "drag" means nothing to jQuery. I am including the draggable="true"
HTML tag in the new element, by the way.
So, anyone have success stories for binding the HTML5 drag events to dynamically created elements, or is it not possible? Thanks!
.on()
binding, which works great for jQuery events. The issue is that I'm trying to achieve this with the HTML5 draggable events. I'll try to clarify my question a bit. Thanks! – Tesslacoileddocument
is often used. See this Demo (drag red square to green one) – Patrick Evans