Utilize drag and drop functionality to interact with an HTML object element

I am struggling with a div that contains a PDF object and draggable text:

<html>
<head>
<script>
function allowDrop(ev) {
  ev.preventDefault();
}


function drop(ev) { alert("DROP"); }
</script>
</head>
<body>


<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)">
<object type="application/pdf"  ondrop="drop(event)" ondragover="allowDrop(event)"
    data="https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf"
    width="80%"
    height="80%">
</object>
</div>
<br>
<div draggable="true">
  <p>Drag me around</p>
</div>
</body>
</html>

The PDF appears correctly, but I am unable to drop items over the PDF object. However, dropping works fine in other areas of the div. Are there limitations regarding drag and drop over HTML object tags? Is there a workaround for this issue?

Answer №1

It's not entirely clear what caused the issue you encountered with dragging and dropping over the PDF object. There are a few potential reasons why this might be happening.

Firstly, PDFs are often treated similarly to iframes and are sandboxed, which means that mouse events may not be detected over them. This could explain why you're unable to detect the mouse position when it's over the PDF.

One workaround for this issue is to position an element on top of the PDF and make that element the droppable area. However, based on your code snippet, it seems like you've already implemented this. Since the PDF is within the parent element #mydiv, there must be another factor causing the problem.

Another possible explanation is that the draggable element is larger than the droppable area. In some cases, the drop will only register if the draggable element is completely within the boundaries of the droppable area.

In conclusion, it's likely that the issue lies somewhere else in your code outside of the provided section. I was able to get it working smoothly without major glitches.

The JavaScript code below works well in Chrome for me. If you're experiencing different results, feel free to share more details so I can investigate further:

$("#Thumbs li").draggable();
$('#mydiv').droppable({
  accept: '#Thumbs li',
  drop: function(event, ui) {
    console.log('Dropped');
  }
});
object {
  border: 1px solid red;
}

#mydiv {
  border: 1px solid green;
  width: 300px;
  height: 300px;
}

#Thumbs li {
  border: 1px solid blue;
  width: 100px; height: 100px;
}
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>

<div id="mydiv">
  <object id="object" data="https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf"
    type="application/pdf" width="100%" height="100%"></object>
</div>

<ul id="Thumbs">
  <li>foo</li>
  <li>bar</li>
  <li>baz</li>
</ul>

Answer №2

The solution turned out to be quite intricate, here's a brief overview:

  • position the pdf element using position:absolute
  • generate a sibling mask div that covers the pdf element with:
    position:absolute;z-index:100;background:transparent;pointer-events:none;
  • make the mask div droppable and implement events for adding/removing pointer-events:
$(`#pdfmask`).droppable({
  accept: ...,
  drop: (event, ui) => ...
  activate: (event, ui) => $(`#pdfmask`).css('pointer-events', 'auto'),
  deactivate: (event, ui) => $(`#pdfmask`).css('pointer-events', 'none'),
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilizing ng-repeat, ng-grid, and ng-click in Angular: A comprehensive guide

I'm encountering an issue with populating a table using ng-repeat after a cellTemplate ng-click. cellTemplate: '<div ng-click="foo()" ng-bind="row.getProperty(col.field)"></div>' Within this foo method, I am tryi ...

Conceal and reveal feature for multiple dynamic identifiers simultaneously

After submitting the form, I am dynamically creating buttons. <template name="workflow"> {{#each newaction}} <div class="btn-box" > {{> actioncardsubcontent}} <button type="button" class="cancelsub" >New Action</button&g ...

Is there a way to store a file in a server directory using the <a href='mypdf.pdf'> tag?

I find myself in a bit of a bind. I have a document that needs to be saved in my server directory, which is attached in an <a href='mypdf.pdf'>My pdf</a> tag. The issue is that the filename, mypdf.pdf, is dynamically changing via Jav ...

Ways to eliminate the gap produced by a fixed element

Why is my chatbox (marked in red) creating unnecessary space at the top (highlighted in yellow), even though it has a sticky position and should not be affecting that area? The other elements on the webpage seem to be impacted by this. How can I prevent th ...

Switching to AngularJS for an upgrade

I am working with an AngularJS code and HTML. <input type="text" ng-model="selected" uib-typeahead="demos for demos in demo | filter:$viewValue | limitTo:8"> $scope.demo = ['demo-NV-enable','demo-NV-disable','demo-NV-shutdo ...

How can we manage a discovered item in Promise and Recursion scenarios without relying on a callback function?

I need to iterate asynchronously and recursively over a folder hierarchy on the server. When a file is found, I want to call a custom method. Here's my current code structure: searchFile(root, handleFile); // recursively loop through folders and ha ...

Oops: Looks like there is already a request for 'PUBLIC_requestAccounts' pending from http://localhost:3000. Just hold tight for now

There comes a moment when an unexpected error arises and fails to establish a connection with your wallet. if (window.ethereum) { console.log("11") const connect = async () => { const account = await window.ethereum.request({ ...

Conceal header and footer bar in Jquery Datatable theme

I need assistance in removing the header/footer bar from this table Here is a visual representation of what I am looking to remove: The jQuery code for this table: $(document).ready(function() { var oTable = $('#tableSmooth').dataTable({ ...

How can I resolve a promise that is still pending within the "then" block?

Here is a piece of code that I have written: fetch(`${URL}${PATH}`) .then(res => { const d = res.json(); console.log("The data is: ", d); return d; }) When the code runs, it outputs The data is: Promise { <pending> ...

What is the alternative method of sending a POST request instead of using PUT or DELETE in Ember?

Is there a way to update or delete a record using the POST verb in Ember RESTAdapter? The default behavior is to send json using PUT or DELETE verbs, but those are blocked where I work. I was wondering if there's a way to mimic Rails behavior by send ...

Switch up your code and toggle a class on or off for all elements that share a specific class

I've been attempting to create a functionality where, upon clicking a switch, a specific class gets added to every element that is assigned the class "ChangeColors". Unfortunately, I have encountered some difficulties in achieving this task. The error ...

Utilizing Bresenham's line drawing algorithm for showcasing box contours

I am seeking a similar behavior to what is demonstrated on , but with some modifications to the boxes: div { display: inline-block; width: 100px; height: 100px; border: 1px solid black; cursor: pointer; } div.selected, div:hover { backgroun ...

Typescript - type assertion does not throw an error for an invalid value

When assigning a boolean for the key, I have to use a type assertion for the variable 'day' to avoid any errors. I don't simply do const day: Day = 2 because the value I receive is a string (dynamic value), and a type assertion is necessary ...

Select a CSS design with C# coding in ASP.net

I have defined two classes in my embedded CSS on the Default.aspx page. If necessary, I can move the code to an external CSS file. Is it possible to write C# code that allows me to select between these two styles for my table after clicking a button? I ha ...

Ensuring Consistent Headings While Scrolling

Imagine having headings structured like this: <h1 class="fixontop">heading 1</h1> content goes here. . . . long paragraph. <h1 class="fixontop">heading 2</h1> 2nd content goes here. . . . long paragraph. <h1 class="fixontop"> ...

Smooth transition of an arrow moving in a continuous loop using CSS animations

I am attempting to create a seamless loop of a single arrow within a div. My approach involves using two SVG arrows in such a way that when the top part of the first arrow is hidden, the corresponding section of the second arrow should be displayed. This ...

The Express app.post endpoint is not acknowledging incoming POST requests from Postman

I am facing an issue where my POST request using Postman to my express app is timing out. Here is the request: https://i.sstatic.net/UfL07.png And here is the app: import express from 'express' import bodyParser from 'body-parser' i ...

What is the best way to modify a state in nextjs?

Recently, I've been working on a next.js project that includes a shopping cart feature. Essentially, when you click on an item, its image is added to a list and displayed with all the other selected items. To remove an item, users can simply click on ...

Querying a map within a Mongo function results in an empty variable when accessed outside of the function, but

Currently, I am facing an issue while trying to create an array of objects using the map function in Mongo and node/express JS. Strangely, when I utilize console.log outside the map function, the array appears empty. However, within the map function, each ...

What are the steps to achieve consistent response behavior in POSTMAN that matches that of a web browser?

Below is an example of my code: const express = require('express'); const app = express(); app.get('/', function (req, res) { res.setHeader('Content-Type', 'text/html'); res.write("First \n"); set ...