• We just launched and are currently in beta. Join us as we build and grow the community.

How to Replace Quote Dynamically by Dragging in JavaScript

Gamerboy123456789

Binary Distribution Expert
G Rep
0
0
0
Rep
0
G Vouches
0
0
0
Vouches
0
Posts
109
Likes
11
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
How to Replace Quote Dynamically by Dragging in JavaScript

Introduction

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 https://getbootstrap.com/.

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. <body

    >
  8. <nav

    class

    =

    "navbar navbar-default"

    >
  9. <div

    class

    =

    "container-fluid"

    >
  10. <a

    class

    =

    "navbar-brand"

    href

    =

    "https://sourcecodester.com"

    >

    Sourcecodester</

    a

    >
  11. </

    div

    >
  12. </

    nav

    >
  13. <div

    class

    =

    "col-md-3"

    ></

    div

    >
  14. <div

    class

    =

    "col-md-6 well"

    >
  15. <h3

    class

    =

    "text-primary"

    >

    How to Replace Quote Dynamically by Dragging in JavaScript</

    h3

    >
  16. <hr

    style

    =

    "border-top:1px dotted #ccc;"

    /

    >
  17. <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

    >
  18. <br

    /

    ><br

    /

    >
  19. <h4

    >

    MY QUOTE</

    h4

    >
  20. <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. }

  4. function

    drop(

    e)

    {
  5. e.preventDefault

    (

    )

    ;
  6. let data=

    e.dataTransfer

    .getData

    (

    "data"

    )

    ;
  7. let word=

    document.getElementById

    (

    data)

    .innerHTML

    ;
  8. e.target

    .innerHTML

    =

    word;
  9. }

  10. function

    drag(

    e)

    {
  11. e.dataTransfer

    .setData

    (

    "data"

    ,

    e.target

    .id

    )

    ;
  12. }

  13. function

    submitQuote(

    )

    {
  14. let myquote=

    document.getElementById

    (

    "my-quote"

    )

    .innerHTML

    ;
  15. alert(

    myquote)

    ;
  16. }

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.

Output:

How%20to%20Replace%20Quote%20Dynamically%20by%20Dragging%20in%20JavaScript%201.png


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


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,292

324,736

324,744

Top