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

How to Transfer Dragged Element to HTML List in JavaScript

abubadr

Anime Merchandise Pro
A Rep
0
0
0
Rep
0
A Vouches
0
0
0
Vouches
0
Posts
83
Likes
138
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
How to Transfer Dragged Element to HTML List in JavaScript

Introduction

In this tutorial we will create a How to Transfer Dragged Element to HTML List in JavaScript. This tutorial purpose is to teach you on how transfer the dragged element into html list. This will tackle all the important functionality that will display the html 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 system or application if you want apply some drag and drop feature. I will give my best to provide you the easiest way of creating this program Transfer Dragged Element into HTML List. So let's do the coding.

Before we get started:

Here 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 for our application. This code will only display the html form inputs and HTML list. 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

    =

    "navabr navbar-default"

    >
  9. <div

    class

    =

    "container-fluid"

    >
  10. <a

    class

    =

    "navbar-brand"

    href

    =

    "https://sourcecodester.com"

    >

    Sourcecodester</

    a

    >
  11. </

    div

    >
  12. </

    nav

    >
  13. <br

    /

    >
  14. <div

    class

    =

    "col-md-3"

    ></

    div

    >
  15. <div

    class

    =

    "col-md-6 well"

    >
  16. <h3

    class

    =

    "text-primary"

    >

    How to Transfer Dragged Element to HTML List</

    h3

    >
  17. <hr

    style

    =

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

    /

    >
  18. <div

    class

    =

    "col-md-6"

    >
  19. <div

    class

    =

    "form-group"

    >
  20. <input

    class

    =

    "form-control"

    type

    =

    "text"

    id

    =

    "data"

    ondrop

    =

    "drop(event)"

    ondragover

    =

    "dragOver(event)"

    readonly

    =

    "readonly"

    /

    >
  21. </

    div

    >
  22. <center><button

    class

    =

    "btn btn-primary"

    onclick

    =

    "transfer();"

    ><span

    class

    =

    "glyphicon glyphicon-save"

    ></

    span

    >

    Transfer</

    button

    >

    <button

    class

    =

    "btn btn-success"

    onclick

    =

    "clearAll();"

    ><span

    class

    =

    "glyphicon glyphicon-refresh"

    ></

    span

    >

    Clear All</

    button

    ></

    center>
  23. <br

    /

    >
  24. <h3

    >

    === MY LIST ===</

    h3

    >
  25. <div

    id

    =

    "result"

    ></

    div

    >
  26. </

    div

    >
  27. <div

    class

    =

    "col-md-6"

    >
  28. <div

    class

    =

    "alert alert-info"

    >

    Movie List</

    div

    >
  29. <h4

    id

    =

    "Little Mermaid"

    draggable

    =

    "true"

    ondragstart

    =

    "drag(event)"

    >

    Little Mermaid</

    h4

    >
  30. <h4

    id

    =

    "Spiderman Into Multiverse"

    draggable

    =

    "true"

    ondragstart

    =

    "drag(event)"

    >

    Spiderman Into Multiverse</

    h4

    >
  31. <h4

    id

    =

    "Avatar 2"

    draggable

    =

    "true"

    ondragstart

    =

    "drag(event)"

    >

    Avatar 2</

    h4

    >
  32. <h4

    id

    =

    "Guardian of the Galaxy"

    draggable

    =

    "true"

    ondragstart

    =

    "drag(event)"

    >

    Guardian of the Galaxy</

    h4

    >
  33. <h4

    id

    =

    "Evil Dead"

    draggable

    =

    "true"

    ondragstart

    =

    "drag(event)"

    >

    Evil Dead</

    h4

    >
  34. </

    div

    >
  35. </

    div

    >
  36. <script

    src

    =

    "script.js"

    ></

    script

    >
  37. </

    body

    >
  38. </

    html

    >

Creating JavaScript Function

This is where the main function of the application is. This code will transfer the dragged element into list when your submit the form. 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. document.getElementById

    (

    'data'

    )

    .value

    =

    data;
  8. }

  9. function

    drag(

    e)

    {
  10. e.dataTransfer

    .setData

    (

    "data"

    ,

    e.target

    .id

    )

    ;
  11. }

  12. function

    transfer(

    )

    {
  13. let data =

    document.getElementById

    (

    'data'

    )

    .value

    ;

  14. if

    (

    data !=

    ""

    )

    {
  15. document.getElementById

    (

    'result'

    )

    .innerHTML

    +=

    data+

    "<br />"

    ;
  16. }
  17. }

  18. function

    clearAll(

    )

    {
  19. document.getElementById

    (

    'result'

    )

    .innerHTML

    =

    ""

    ;
  20. document.getElementById

    (

    'data'

    )

    .value

    =

    ""

    ;
  21. }

This code above uses only one method to transfer the element into html list called transfer(). This function will transfer the dragged element into html list after you submit the form inputs. To transfer a dragged element we just simply get the dragged data via the drag event then put it into the html form in order to submit the inputs.

Output:

How%20to%20Transfer%20Dragged%20Element%20to%20HTML%20List%20in%20JavaScript%201.png


The How to Transfer Dragged Element to HTML List 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 Transfer Dragged Element to HTML List 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 hidden text.
 

450,053

322,753

322,762

Top