Learn how to leverage the jQuery Draggable plugin to easily create draggable elements and initiate dragging using the HTML5 native Drag and Drop

My goal is to create a unique functionality using the HTML5 Drag And Drop API and jQuery. I want to trigger a dragover event, then have a jQuery draggable object follow the mouse until the dragend event occurs.

The reason for this is that I am working on an application that relies on the jQuery.ui draggable feature for a specific plugin (FullCalendar Scheduler v3). I aim to enhance the application by allowing users to drag and drop items from one browser window to another within the mentioned plugin.

Since the plugin does not support the native HTML5 Drag and Drop API and jQuery.ui draggable cannot handle cross-window dragging, my solution involves combining these two plugins.

My proposed approach is to utilize the HTML5 Drag and Drop API to create a new draggable element in the target browser window when a dragged item enters a dropzone. By simulating a mousedown event on the new draggable element, it will track the cursor's movement. Upon triggering the dragend event, I plan to simulate a mouseup event on the draggable element, enabling the scheduler plugin to function accordingly.

To test this method initially with a single browser window, I attempted to implement the first part of my solution: creating a jQuery.ui draggable element and initiating its movement via a simulated mousedown event during a dragover event. However, I encountered difficulties achieving the desired behavior.

I provided a fiddle showcasing my attempts thus far (full code not included here): JSFiddle

Unfortunately, I encountered a type.indexOf is not a function error while testing both options in the Fiddle.

I sought assistance on a related question where a solution was proposed for starting the drag operation with a click event, but other event types did not yield the same result. It seems a mousedown.draggable event can only be simulated from a MouseEvent, which the dragend event is not.

In essence, I require guidance to successfully implement the intended functionality, especially for the initial phase of my proposed solution!

Answer №1

Finding a solution for this issue seems to be quite complex. The inconsistency in DnD terminology and functionality across different browsers adds another layer of difficulty. For example, FireFox triggers a dragenter event upon drop, while Chrome fails to detect a drop event from objects originating from another window.

In my attempt to address this problem, I conducted some testing. To replicate the scenario, I suggest copying the content into a Text file and saving it as HTM or HTML. Then, proceed to open the file locally in your browser. Next, open another window and access the second HTM file. By following these steps, you should have two windows where dragging elements between them is possible.

wina-1.htm

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Window A</title>
  <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <style>
  .items {
    position: relative;
  }
  .items > div {
    margin-right: 5px;
    width: 150px;
    height: 150px;
    padding: 0.5em;
    border-radius: 6px;
    display: inline-block;
  }
  #log {
    width: 100%;
    height: 5em;
    overflow-y: auto;
  }
  [draggable].idle {
    background-color: rgba(255,0,0,0.75); 
  }
  [draggable].selected {
    background-color: rgba(255,0,0,0.95);
  }
  </style>
</head>
...
...

<p>The use of raw JavaScript is emphasized in this setup. While experimenting with jQuery UI, retaining the stylesheet was done for simplified theming purposes. Notably, there are sections allocated for logging details, draggable elements, and static items within the setup.</p>

<p><strong>winb-1.htm</strong></p>
...
...

<p>jQuery was incorporated in Window B with an intent to enhance the element's functionality by transforming it into a jQuery UI Draggable entity.</p>

<p>One crucial aspect to bear in mind is the limitation on alterations during transit. Since the source element remains detached from the target DOM, modifications cannot occur en route. Instead, initializing a new draggable element is advised within the <code>drop event, creating a fresh item assigned with all relevant data at that point.

Additionally, considering the unreliability of data transfer, utilizing localStorage over DataTransfer may prove more efficient and dependable. This method mirrors a cookie-like behavior, boasting higher reliability.

To illustrate, an exemplary Data object has been crafted:

... ...

This approach should ideally function seamlessly. However, practical implementation might encounter various obstacles along the way. A suggestion is proposed for a click-to-copy mechanism, where users select an item in Window A before placing it into Window B through localStorage, enabling the duplication of the item at the new location.

wina-3.htm

... ...

winb-3.htm

... ...

While acknowledging that this response may not align precisely with your query, I urge you to provide further clarity in formulating your question. It appears you are circling around the main issue without addressing it directly.

Wishing you success in tackling this challenge!

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

How to loop through a JSON object using AngularJS

I have a nested JSON "object" called znanja that I want to showcase on my webpage. It contains various values that I wish to present in the form of a list. Essentially, I am constructing a portfolio to highlight my skills. So far, I've been able to di ...

AngularJS - displaying a notification when the array length is empty and customizing the visibility to conceal the default action

I've been working on an Angular project where I display a loading circle that disappears once the content is loaded. This circle is simply a CSS class that I call from my HTML only when the page first loads, like this: Actually, the circle consists o ...

What is the process for displaying user input on the console?

How can I ensure that the server is receiving user input? What steps should I take to print this input to the console? Currently, the console.log statement only displays "About to create new room", but I require the user input as well. Client-Side: // C ...

Setting up TailwindCSS in Nuxt3: A step-by-step guide

Looking to customize the default font Proxima Nova from TailwindCSS in my Nuxt3 project but navigating the file structure is a bit tricky for me. I've gone ahead and installed the tailwindcss module: npm i -D @nuxtjs/tailwindcss and added the module ...

How can I create an Onclick function that works for multiple buttons sharing the same ID?

Having an issue with a component that loads data using buttons. I have a function that updates the state/variable based on the button ID, but since all button IDs are the same, it only works for the first button... I'm struggling to assign unique IDs ...

Attempting to store user information in local storage was unsuccessful in my attempt

Hi everyone, I need some assistance with troubleshooting this code as I've done my best but can't seem to find a solution. My goal is to store the user information entered in a form to the local storage so that I can use it for the active session ...

Notifying the Player of Victory in a Javascript Tic Tac Toe Game: The Winning Alert

Recently, I decided to dive into Javascript and kick off my very first game project - a Tic Tac Toe game. If you're curious to see the code for my project, you can check it out here: Tic Tac Toe Project One feature I'm eager to implement is a n ...

Using a custom TypeScript wrapper for Next.js GetServerSideProps

I developed a wrapper for the SSR function GetServerSideProps to minimize redundancy. However, I am facing challenges in correctly typing it with TypeScript. Here is the wrapper: type WithSessionType = <T extends {}>( callback: GetServerSideProps&l ...

Transforming S3 Buffer Information into a PDF Document

Utilizing an express route to fetch the s3 object through the AWS SDK: router.get('/myRoute', (req, res) => { const s3 = new AWS.S3({apiVersion: '2006-03-01'}); s3.getObject({Bucket: 'my-bucket', Key: 'my-key'}, ...

Issue arises when isomorphic-dompurify is used alongside dompurify in Next.js 13 causing compatibility problems

I am currently facing a compatibility problem involving isomorphic-dompurify and dompurify in my Next.js 13 project. It appears that both libraries are incompatible due to their dependencies on canvas, and I am struggling to find a suitable alternative. M ...

JavaScript - Removing Content from an Element

I have a coding script that adds an image preview of uploaded images. The issue I am facing is that the previous image does not clear when the form is filled out again. The id of the div where thumbnail images are being displayed is "thumbnail". Can someo ...

Using JavaScript and the Firefox browser, learn how to easily highlight an element with Selenium-WebDriver

I am struggling with creating a valid function to highlight specific elements on a webpage. As a beginner in coding, I suspect that the issue may either be related to my environment setup or a lack of knowledge about JavaScript/Selenium features. I am wri ...

Generating Angular select options dynamically using ng-repeat

I've been struggling for several days now with ngOptions and ngRepeat. I am trying to create a select option for a people control. The REST service is returning an array as shown below. Despite trying various solutions from Stack Overflow and the Angu ...

Customize bullet list icons to adjust in size based on content using css

In my CMS project, the CMS team has a special requirement regarding unordered and ordered lists. They want the size of the bullet list icon to adjust according to the text content within the list. The image below shows the default design of a list item: ...

Determining the file size of an HTML and JavaScript webpage using JavaScript

Is there a way to determine the amount of bytes downloaded by the browser on an HTML+JS+CSS page with a set size during page load? I am looking for this information in order to display a meaningful progress bar to the user, where the progress advances bas ...

Merge two JSON objects together by matching their keys and maintaining the original values

After having two separate JSON objects representing data, I found myself in the position of needing to merge them into one combined result. The initial objects were as follows: passObject = { 'Topic One' : 4, 'Topic Two' : 10, &apos ...

Need help with updating React component state within a Meteor.call callback?

I've constructed this jsx file that showcases a File Uploading Form: import React, { Component } from 'react'; import { Button } from 'react-bootstrap'; class UploadFile extends Component { constructor(){ super() this. ...

Dynamically load directives through the use of ng-repeat

Creating a custom Dropdown menu by wrapping the HTML "select" element in my directive. This directive will display a list of options in the Dropdown menu. The select element is tagged with my custom "arBootstrapSelect" directive/attribute. This directive ...

Choose particular spreadsheets from the office software

My workbook contains sheets that may have the title "PL -Flat" or simply "FLAT" I currently have code specifically for the "PL -Flat" sheets, but I want to use an if statement so I can choose between either sheet since the rest of the code is identical fo ...

Guide to verifying a value within a JSON object in Ionic 2

Is there a way to check the value of "no_cover" in thumbnail[0] and replace it with asset/sss.jpg in order to display on the listpage? I have attempted to include <img src="{{item.LINKS.thumbnail[0]}}"> in Listpage.html, but it only shows the thumbna ...