jquery - Javascript: How to get the text value of drop event? -


i wondered how text value of drop event. seems easy question surprisingly can't find answer works.

demo below

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> <div id="dropbox" contenteditable="true" id="dropbox"> </div>  <style> #dropbox{ width:400px; height:60px; background-color: #87cefa; } </style>  <script> $('#dropbox').on('drop', function(e) {  console.log(e.datatransfer); //first try console.log(e.getdata('text'));  //second try  });      </script> 

first, it's idea wrap dom-manipulating code in ready call, page ready before start manipulating it. see link more details: https://learn.jquery.com/using-jquery-core/document-ready/

then, code should work:

var dropstr; $( document ).ready(function() {     $('#dropbox').on('drop', function(e) {         e.originalevent.datatransfer.items[0].getasstring(function(str)         {             dropstr=str;          })      }); }); 

the datatransfer api bit involved, can find more details here: https://developer.mozilla.org/en-us/docs/web/api/datatransfer


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -