Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1070240 of DataTransfer.getData()

  • Revision slug: Web/API/DataTransfer/getData
  • Revision title: DataTransfer.getData()
  • Revision id: 1070240
  • Created:
  • Creator: brulecap
  • Is current revision? No
  • Comment

Revision Content

 

{{APIRef("HTML DOM")}}

The DataTransfer.getData() method retrieves drag data (as a {{domxref("DOMString")}}) for the specified type. If the drag operation does not include data, this method returns an empty string.

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

Syntax

DOMString dataTransfer.getData(format);

Arguments

format
A {{domxref("DOMString")}} representing the type of data to retrieve.

Return value

{{domxref("DOMString")}}
A {{domxref("DOMString")}} representing the drag data for the specified format. If the drag operation has no data or the operation has no data for the specified format, this method returns an empty string.

Example

This example shows the use of the {{domxref("DataTransfer")}} object's {{domxref("DataTransfer.getData()","getData()")}} and {{domxref("DataTransfer.setData()","setData()")}} methods.

HTML Content

<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"><span id="drag4" draggable="true" ondragstart="drag(event)">drag me to the other box</span></div>
<div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)"></div>

CSS Content

#div1, #div2 {width:100px;height:50px;padding:10px;border:1px solid #aaaaaa;}

JavaScript Content

function allowDrop(allowdropevent) {
    allowdropevent.target.style.color = 'blue';
    allowdropevent.preventDefault();
}

function drag(dragevent) {
    dragevent.dataTransfer.setData("text", dragevent.target.id);
    dragevent.target.style.color = 'green';
}

function drop(dropevent) {
    dropevent.preventDefault();
    var data = dropevent.dataTransfer.getData("text");
    dropevent.target.appendChild(document.getElementById(data));
    dropevent.target.firstChild.style.color = 'black';
}

Result

{{ EmbedLiveSample('Example', 600, '', '', 'Web/API/DataTransfer/getData') }}

Specifications

Specification Status Comment
{{SpecName("HTML WHATWG", "interaction.html#dom-datatransfer-getdata", "getData()")}} {{Spec2("HTML WHATWG")}}  
{{SpecName("HTML5.1", "editing.html#dom-datatransfer-getdata", "getData()")}} {{Spec2("HTML5.1")}} Initial definition

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 4 3.5 {{CompatGeckoDesktop(10)}} [1] 12 3.1
Feature Android Android Webview Chrome for Android Firefox Mobile (Gecko) Firefox OS IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatGeckoMobile(10)}}[1] {{CompatNo}} {{CompatIE("10")}} {{CompatNo}} {{CompatNo}}

[1] Before Firefox 49, this method always returned an empty list if the mime type was not in a white list.

See also

{{page("/en-US/docs/Web/API/DataTransfer", "See also")}}

Revision Source

<div>
<p>&nbsp;</p>
{{APIRef("HTML DOM")}}</div>

<p>The <strong><code>DataTransfer.getData()</code></strong> method retrieves drag data (as a {{domxref("DOMString")}}) for the specified type. If the drag operation does not include data, this method returns an empty string.</p>

<p>Example data types are <code>text/plain</code> and <code>text/uri-list</code>.</p>

<h2 id="Syntax">Syntax</h2>

<pre class="syntaxbox">
DOMString <var>dataTransfer</var>.getData(format);
</pre>

<h3 id="Arguments">Arguments</h3>

<dl>
 <dt><em>format</em></dt>
 <dd>A {{domxref("DOMString")}} representing the type of data to retrieve.</dd>
</dl>

<h3 id="Return_value">Return value</h3>

<dl>
 <dt>{{domxref("DOMString")}}</dt>
 <dd>A {{domxref("DOMString")}} representing the drag data for the specified <code>format</code>. If the drag operation has no data or the operation has no data for the specified <code>format</code>, this method returns an empty string.</dd>
</dl>

<h2 id="Example">Example</h2>

<p>This example shows the use of the {{domxref("DataTransfer")}} object's {{domxref("DataTransfer.getData()","getData()")}} and {{domxref("DataTransfer.setData()","setData()")}} methods.</p>

<h3 id="HTML_Content">HTML Content</h3>

<pre class="brush: html">
&lt;div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"&gt;&lt;span id="drag4" draggable="true" ondragstart="drag(event)"&gt;drag me to the other box&lt;/span&gt;&lt;/div&gt;
&lt;div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)"&gt;&lt;/div&gt;
</pre>

<h3 id="CSS_Content">CSS Content</h3>

<pre class="brush: css">
#div1, #div2 {width:100px;height:50px;padding:10px;border:1px solid #aaaaaa;}
</pre>

<h3 id="JavaScript_Content">JavaScript Content</h3>

<pre class="brush: js">
function allowDrop(allowdropevent) {
    allowdropevent.target.style.color = 'blue';
    allowdropevent.preventDefault();
}

function drag(dragevent) {
    dragevent.dataTransfer.setData("text", dragevent.target.id);
    dragevent.target.style.color = 'green';
}

function drop(dropevent) {
    dropevent.preventDefault();
    var data = dropevent.dataTransfer.getData("text");
    dropevent.target.appendChild(document.getElementById(data));
    dropevent.target.firstChild.style.color = 'black';
}
</pre>

<h3 id="Result">Result</h3>

<p id="EmbedLiveSample('Example'_''_''_''_'WebAPIDataTransfergetData')">{{ EmbedLiveSample('Example', 600, '', '', 'Web/API/DataTransfer/getData') }}</p>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName("HTML WHATWG", "interaction.html#dom-datatransfer-getdata", "getData()")}}</td>
   <td>{{Spec2("HTML WHATWG")}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName("HTML5.1", "editing.html#dom-datatransfer-getdata", "getData()")}}</td>
   <td>{{Spec2("HTML5.1")}}</td>
   <td>Initial definition</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari (WebKit)</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>4</td>
   <td>3.5</td>
   <td>{{CompatGeckoDesktop(10)}} [1]</td>
   <td>12</td>
   <td>3.1</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Android Webview</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>Firefox OS</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatGeckoMobile(10)}}[1]</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatIE("10")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
 </tbody>
</table>
</div>

<p>[1] Before Firefox 49, this method always returned an empty list if the mime type was not in a white list.</p>

<h2 id="See_also">See also</h2>

<p>{{page("/en-US/docs/Web/API/DataTransfer", "See also")}}</p>
Revert to this revision