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

JavaScript - Store Element Value When Drag

hyytyg

Cyber Analyst
H Rep
0
0
0
Rep
0
H Vouches
0
0
0
Vouches
0
Posts
119
Likes
110
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 100 XP
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 https://getbootstrap.com/.

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

    >
  8. <nav class

    =

    "navabr 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"

    >

    JavaScript - Store Element Value When Drag</

    h3

    >
  16. <hr

    style

    =

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

    /

    >
  17. <div

    class

    =

    "col-md-6"

    >
  18. <div

    class

    =

    "alert alert-info"

    >

    Movie List</

    div

    >
  19. <h4

    id

    =

    "Iron Man"

    draggable=

    "true"

    ondragstart=

    "drag(event)"

    >

    Iron Man</

    h4

    >
  20. <h4

    id

    =

    "Frozen 2"

    draggable=

    "true"

    ondragstart=

    "drag(event)"

    >

    Frozen 2</

    h4

    >
  21. <h4

    id

    =

    "Hotel Transylvania"

    draggable=

    "true"

    ondragstart=

    "drag(event)"

    >

    Hotel Transylvania</

    h4

    >
  22. <h4

    id

    =

    "Venom"

    draggable=

    "true"

    ondragstart=

    "drag(event)"

    >

    Venom</

    h4

    >
  23. <h4

    id

    =

    "Aladin"

    draggable=

    "true"

    ondragstart=

    "drag(event)"

    >

    Aladin</

    h4

    >
  24. </

    div

    >
  25. <div

    class

    =

    "col-md-6"

    >
  26. <div

    class

    =

    "form-group"

    >
  27. <input

    class

    =

    "form-control"

    type

    =

    "text"

    id

    =

    "data"

    ondrop=

    "drop(event)"

    ondragover=

    "dragOver(event)"

    readonly

    =

    "readonly"

    /

    >
  28. </

    div

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

    >
  30. <br

    /

    >
  31. <div

    id

    =

    "result"

    ></

    div

    >
  32. </

    div

    >
  33. </

    div

    >
  34. <script

    src

    =

    "js/script.js"

    ></

    script

    >
  35. </

    body

    >
  36. </

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

  4. function

    drop(

    e)

    {
  5. e.preventDefault

    (

    )

    ;
  6. var

    data =

    e.dataTransfer

    .getData

    (

    "data"

    )

    ;

  7. document.getElementById

    (

    'data'

    )

    .value

    =

    data;
  8. }

  9. function

    drag(

    e)

    {
  10. e.dataTransfer

    .setData

    (

    "data"

    ,

    e.target

    .id

    )

    ;
  11. }

  12. function

    storeItems(

    )

    {
  13. var

    data =

    document.getElementById

    (

    'data'

    )

    .value

    ;

  14. if

    (

    data !=

    ""

    )

    {
  15. document.getElementById

    (

    'result'

    )

    .innerHTML

    +=

    data+

    "<br />"

    ;
  16. }
  17. }

  18. function

    reset(

    )

    {
  19. document.getElementById

    (

    'result'

    )

    .innerHTML

    =

    ""

    ;
  20. document.getElementById

    (

    'data'

    )

    .value

    =

    ""

    ;
  21. }

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!


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

452,496

338,535

338,543

Top