Skip to main content

JavaScript - Store Element Value When Drag

In this tutorial we will create a Store Element Value When Drag using JavaScript. This code will dynamically store an element value when drop in the form inputs. The code use JavaScript drag and drop feature in order to store element value by providing an id in the ondragstart() event. A user-friendly program so that you can modify and understand it without an ease.

We will be using JavaScript as a server-side scripting language that interpret a program to extend a whole new level of HTML experience. A scripting language that use in different ways that provide a whole level of experience when visiting a website.

Getting started:

First you have to download bootstrap framework, this is the link for the bootstrap that I used for the layout design

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTf-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <nav class="navabr navbar-default">
  8. <div class="container-fluid">
  9. <a class="navbar-brand" href="">Sourcecodester</a>
  10. </div>
  11. </nav>
  12. <div class="col-md-3"></div>
  13. <div class="col-md-6 well">
  14. <h3 class="text-primary">JavaScript - Store Element Value When Drag</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-6">
  17. <div class="alert alert-info">Movie List</div>
  18. <h4 id="Iron Man" draggable="true" ondragstart="drag(event)">Iron Man</h4>
  19. <h4 id="Frozen 2" draggable="true" ondragstart="drag(event)">Frozen 2</h4>
  20. <h4 id="Hotel Transylvania" draggable="true" ondragstart="drag(event)">Hotel Transylvania</h4>
  21. <h4 id="Venom" draggable="true" ondragstart="drag(event)">Venom</h4>
  22. <h4 id="Aladin" draggable="true" ondragstart="drag(event)">Aladin</h4>
  23. </div>
  24. <div class="col-md-6">
  25. <div class="form-group">
  26. <input class="form-control" type="text" id="data" ondrop="drop(event)" ondragover="dragOver(event)" readonly="readonly"/>
  27. </div>
  28. <center><button class="btn btn-primary" onclick="storeItems();"><span class="glyphicon glyphicon-save"></span> Store</button> <button class="btn btn-success" onclick="reset();"><span class="glyphicon glyphicon-refresh"></span> Reset</button></center>
  29. <br />
  30. <div id="result"></div>
  31. </div>
  32. </div>
  33. <script src="js/script.js"></script>
  34. </body>
  35. </html>

Creating the Script

This code contains the script of the application. This code will store element value when drop the target zone. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.

  1. function dragOver(e){
  2. e.preventDefault();
  3. }
  5. function drop(e){
  6. e.preventDefault();
  7. var data = e.dataTransfer.getData("data");
  9. document.getElementById('data').value = data;
  10. }
  12. function drag(e){
  13. e.dataTransfer.setData("data",;
  14. }
  16. function storeItems(){
  17. var data = document.getElementById('data').value;
  19. if(data !=""){
  20. document.getElementById('result').innerHTML += data+"<br />";
  21. }
  22. }
  24. function reset(){
  25. document.getElementById('result').innerHTML = "";
  26. document.getElementById('data').value = "";
  27. }

There you have it we successfully created a Store Element Value When Drag using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.


1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.