The DataTransfer.setData() method sets the drag operation's drag data to the specified data and type. If data for the given type does not exist, it is added at the end of the drag data store, such that the last item in the types list will be the new type. If data for the given type already exists, the existing data is replaced in the same position. That is, the order of the types list is not changed when replacing data of the same type.

Example data types are text/plain and text/uri-list.


void dataTransfer.setData(format, data);


A DOMString representing the type of the drag data to add to the drag object.
A DOMString representing the data to add to the drag object.

Return value



This example shows the use of the DataTransfer object's getData(), setData() and clearData() methods.

<!DOCTYPE html>
<html lang=en>
<title>Examples of DataTransfer's setData(), getData() and clearData()</title>
<meta content="width=device-width">
  div {
    margin: 0em;
    padding: 2em;
  #source {
    color: blue;
    border: 1px solid black;
  #target {
    border: 1px solid black;
function dragstart_handler(ev) {
 // Change the source element's background color to signify drag has started = "dashed";
 // Set the drag's format and data. Use the event target's id for the data 

function dragover_handler(ev) {

function drop_handler(ev) {
 // Get the data, which is the id of the drop target
 var data = ev.dataTransfer.getData("text");;
 // Clear the drag data cache (for all formats/types)
<h1>Examples of <code>DataTransfer</code>: <code>setData()</code>, <code>getData()</code>, <code>clearData()</code></h1>
   <p id="source" ondragstart="dragstart_handler(event);" draggable="true">
     Select this element, drag it to the Drop Zone and then release the selection to move the element.</p>
 <div id="target" ondrop="drop_handler(event);" ondragover="dragover_handler(event);">Drop Zone</div>


Specification Status Comment
HTML Living Standard
The definition of 'setData()' in that specification.
Living Standard  
HTML 5.1
The definition of 'setData()' in that specification.
Recommendation Initial definition

Browser compatibility

Update compatibility data on GitHub
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
setDataChrome Full support YesEdge Full support 12Firefox Full support YesIE ? Opera Full support YesSafari Full support YesWebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS No support NoSamsung Internet Android Full support Yes


Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown

See also