DataTransfer: addElement() method

فهرست فصل‌ها دانلود آرشیوناشر

MDN Web Docs · نسخهٔ df622a2a1c1e · CC BY-SA 2.5 و مجوز جداگانهٔ ناشر برای نمونه‌کد

{{APIRef("HTML Drag and Drop API")}}{{SeeCompatTable}}{{Non-standard_header}}

The **addElement()** method of the {{domxref("DataTransfer")}} interface sets the drag source to the given element. This element will be the element to which {{domxref("HTMLElement/drag_event", "drag")}} and {{domxref("HTMLElement/dragend_event", "dragend")}} events are fired, and not the default target (the node that was dragged).

During a drag operation, this method can only be used in the handler for the {{domxref("HTMLElement/dragstart_event", "dragstart")}} event, because that's the only time the drag operation's data store is writable. Calling it from any other drag event throws a NoModificationAllowedError {{domxref("DOMException")}}. See Modifying the drag data store for details.

Syntax

js-nolint
addElement(element)

Parameters

  • element
  • : The {{domxref("Element")}} to set as the drag source.

Return value

None ({{jsxref("undefined")}}).

Exceptions

  • NoModificationAllowedError {{domxref("DOMException")}}
  • : Thrown if the drag data store is not in read/write mode.

Examples

This example shows the use of the addElement() method

js
function changeDragNode(event, node) {
  const dt = event.dataTransfer;
  dt.addElement(node);
}

Specifications

This method is not defined in any Web standard.

Browser compatibility

{{Compat}}

See also