How to Replace Quote Dynamically by Dragging in JavaScript

How to Replace Quote Dynamically by Dragging in JavaScript


In this tutorial we will create a How to Replace Quote Dynamically by Dragging in JavaScript. This tutorial purpose is to allow you to replace any quotes you want by dragging. This will cover all the basic function that will search a list. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any application if you want to have an interactive feature of dragging things. I will give my best to provide you the easiest way of creating this program Replace Quote by dragging. So let's do the coding.

Before we get started:

This is the link for the template that i used for the layout design

Creating The Interface

This is where we will create a simple interface to our application. This code will several quotes and buttons To create this simply copy and write it into your text editor, then save it 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="navbar 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">How to Replace Quote Dynamically by Dragging in JavaScript</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <a href="index.html" class="btn btn-success pull-left"><span class="glyphicon glyphicon-refresh"></span> Reset</a>&nbsp;<button type="button" class="btn btn-primary" onclick="submitQuote();">Submit</button>
  17. <br /><br />
  18. <h4>MY QUOTE</h4>
  19. <div id="my-quote" class="col-md-6" style="border:1px SOLID #ccc; height:100px;" ondrop="drop(event)" ondragover="dragOver(event)">“If you’re going through hell, keep going.”</div>
  21. <div class="col-md-6">
  22. <h4>List of Quotes</h4>
  23. <div class="alert alert-success" draggable="true" ondragstart="drag(event)" id="word1">“Don’t let yesterday take up too much of today.”</div>
  24. <div class="alert alert-success" draggable="true" ondragstart="drag(event)" id="word2">“Every man dies. Not every man lives.”</div>
  25. <div class="alert alert-success" draggable="true" ondragstart="drag(event)" id="word3">“Life shrinks or expands in proportion to one’s courage.”</div>
  26. <div class="alert alert-success" draggable="true" ondragstart="drag(event)" id="word4">“Nothing is impossible. The word itself says “I’m possible!”</div>
  27. </div>
  28. </div>
  29. <script src="script.js"></script>
  30. </body>
  31. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will dynamically replace your quote by dragging it to the drop zone. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. function dragOver(e){
  2. e.preventDefault();
  3. }
  5. function drop(e){
  6. e.preventDefault();
  7. let data=e.dataTransfer.getData("data");
  8. let word=document.getElementById(data).innerHTML;
  9. = word;
  10. }
  12. function drag(e){
  13. e.dataTransfer.setData("data",;
  14. }
  16. function submitQuote(){
  17. let myquote=document.getElementById("my-quote").innerHTML;
  18. alert(myquote);
  19. }

In the code above we just simply create a method called dragOver(), this function will prevent any default function of the web browser. Next we will create the drag() method, this function will carry over the data that you been dragging. Lastly we will create the drop() method, this function will get the dragged element data and append it into the targeted div object.


The How to Replace Quote Dynamically by Dragging in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Replace Quote Dynamically by Dragging in 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!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment