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

  • Home
  • SEARCH
  • 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 8241625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:53:39+00:00 2026-06-07T20:53:39+00:00

I have created two canvases in a div using Javascript. I have another div

  • 0

I have created two canvases in a div using Javascript. I have another div with an image. I want to drop that image into the canvas. But I am getting error in adding events to the canvases. I am trying to set the events “ondragover” and “ondrop”. But I am getting the error that event is not defined.

Here is my code:

<html>
    <head> 
    <script type="text/javascript">

    function dragIt(theEvent) {
theEvent.dataTransfer.setData("Text", theEvent.target.id);   
}

function dropIt(theEvent) {
var theData = theEvent.dataTransfer.getData("Text");
dt = document.getElementById(theData);
theEvent.preventDefault();  
}    

window.onload = function() {     
  var c2 = document.createElement('canvas');
  c2.width = 140;
  c2.height = 140;
  c2.style.cssText = 'position:relative;top:70px;left:498px;border:2px  solid black;';
  var ctx2 = c2.getContext('2d');    
  document.body.appendChild(c2);
  c2.ondragover=event.preventDefault();
  c2.ondrop=dropIt(event);


  var c3 = document.createElement('canvas');
  c3.width = 140;
  c3.height = 140;
  c3.style.cssText = 'position:relative;top:70px;left:502px; border:2px  solid black;';
  var ctx3 = c3.getContext('2d');
  document.body.appendChild(c3);     

  };  
</script>

</head>
    <body>
    <div class="pics">
    <div id="place1">
    <img src="images/211.jpg" width="80" height="80" draggable="true" ondragstart="dragIt(event);" id="pic1" />
    </div>   
    <div style = "position:absolute;top:150px;width:300;height:400;left:500px; border:2px  solid black;"></div>
    </body>
</html>
  • 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-06-07T20:53:40+00:00Added an answer on June 7, 2026 at 8:53 pm
    c2.ondragover=event.preventDefault();
    c2.ondrop=dropIt(event);
    

    These lines will execute some functions, and assign the result of that as an event listener. That’s not what you want, you need to assign the function object itself. Also, the execution will fail because there is no event object that time – it will be created somewhen in the future, when the drag really happens, and be passed to the function as an argument.

    c2.ondragover = function(event) {
      event.preventDefault();
    };
    c2.ondrop = dropIt;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created two viewController classes such that one is superclass of another.i have
I have created two WindowSurface (WPF), and I want to navigate betwen them. I've
I have created two JavaScript files. One file is "validators.js" and the other is
I have created two classes, class A that uses a Linked List and class
I have created two enums and I know they are not the same but
I have created two layout files (in res/layout and res/layout-land) for my application. But
I have created two threads and i want to see their priority after they
I have created two forms in C# using Add new item. Form1 is my
I have created two tabs in my Activity using the below code. This is
I have created two brand new solutions using the latest VS 2010: C# console

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.