Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6822999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:43:49+00:00 2026-05-26T21:43:49+00:00

Trying to do a simple drag and drop example in HTML5 – but when

  • 0

Trying to do a simple drag and drop example in HTML5 – but when I drop the image into the div element I get the following error.

Uncaught TypeError: Cannot set property ‘innerHTML’ of null

So I assume the error message means dragElement is null. I don’t understand why though, because I set it in the dragstart event to be the HTML of the img element.

Anyone know how to make this work?

var dragElement = null;

$('#x').bind('dragstart', function (e) {
    dragElement = this;
    e.dataTransfer.effectAllowed = 'move';
    e.dataTransfer.setData('text/html', this.innerHTML);
});

$('#drop-box').bind('dragover', function (e) {
    e.preventDefault();
    return false;
});

$('#drop-box').bind('drop', function (e) {
    e.preventDefault();
    if (e.stopPropagation) {
        e.stopPropagation();
    }

    if (dragElement != this) {
        dragElement.innerHTML = this.innerHTML;
        this.innerHTML = e.originalEvent.dataTransfer.getData('text/html');
    }

    return false;
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:43:50+00:00Added an answer on May 26, 2026 at 9:43 pm

    dataTransfer is part of the original event object, not the jQuery one. Use e.originalEvent instead: http://jsfiddle.net/KWut6/.

     e.originalEvent.dataTransfer ...
    

    HTML

    <image src="http://lorempixum.com/100/100/" draggable="true" id="x">
    <div id="drop-box">a</div>
    

    JavaScript

    var dragElement = null;
    
    $('#x').bind('dragstart', function (e) {
        dragElement = this;
        e.originalEvent.dataTransfer.effectAllowed = 'move';
        e.originalEvent.dataTransfer.setData('text/html', this.innerHTML);
    });
    
    $('#drop-box').bind('dragover', function (e) {
        e.preventDefault();
        return false;
    });
    
    $('#drop-box').bind('drop', function (e) {
        e.preventDefault();
        if (e.stopPropagation) {
            e.stopPropagation();
        }
    
        if (dragElement != this) {
            dragElement.innerHTML = this.innerHTML;
            this.innerHTML = e.originalEvent.dataTransfer.getData('text/html');
        }
    
        return false;
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple example where I am trying to drag an element inside
I'm trying to drag and drop an image from one spot on the canvas
I'm using CommonsGuy's drag n drop example and I am basically trying to integrate
=I am trying to develop a simple drag/drop UI in my web application. An
I am trying to perform a simple drag-n-drop operation starting from one button in
I'm trying to implement a simple drag/drop of images between panels using jQuery. While
I'm trying to make an applet which I can simply drag an image. And
I am trying a simple Extension Method example and am unable to increment or
I am trying something very simple, but for some reason it does not work.
I am trying to get simple jQuery to execute on my Content page with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.