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

Advertise Here

Advertise Here

Advertise Here

Drag and Drop Change Background Color in JavaScript

Rrb

Infrastructure Optimizer
R Rep
0
0
0
Rep
0
R Vouches
0
0
0
Vouches
0
Posts
54
Likes
188
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 100 XP
Drag and Drop Change Background Color in JavaScript

Introduction

In this tutorial we will create a Drag and Drop Change Background Color in JavaScript. This tutorial purpose is to teach how to use the drag and drop feature. This will cover all the basic functions that will make this program works. 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 kind of system that needed to implement a drag and drop function. I will give my best to provide you the easiest way of creating this program Drag and Drop. 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 for our application. This code will display only a box and several colored boxes. 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"

    >

    Drag and Drop Change Background Color</

    h3

    >
  16. <hr

    style

    =

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

    /

    >
  17. <div

    class

    =

    "col-md-6"

    >
  18. <div

    class

    =

    "col-md-12"

    >
  19. <div

    style

    =

    "width:50px; height:50px; border:1px solid red; background-color:red;"

    id

    =

    "red"

    draggable

    =

    "true"

    ondragstart

    =

    "drag(event)"

    ></

    div

    >
  20. <br

    /

    >
  21. <div

    style

    =

    "width:50px; height:50px; border:1px solid yellow; background-color:yellow;"

    id

    =

    "yellow"

    draggable

    =

    "true"

    ondragstart

    =

    "drag(event)"

    ></

    div

    >
  22. <br

    /

    >
  23. <div

    style

    =

    "width:50px; height:50px; border:1px solid blue; background-color:blue;"

    id

    =

    "blue"

    draggable

    =

    "true"

    ondragstart

    =

    "drag(event)"

    ></

    div

    >
  24. <br

    /

    >
  25. <div

    style

    =

    "width:50px; height:50px; border:1px solid green; background-color:green;"

    id

    =

    "green"

    draggable

    =

    "true"

    ondragstart

    =

    "drag(event)"

    ></

    div

    >
  26. <br

    /

    >
  27. <div

    style

    =

    "width:50px; height:50px; border:1px solid black; background-color:black;"

    id

    =

    "black"

    draggable

    =

    "true"

    ondragstart

    =

    "drag(event)"

    ></

    div

    >
  28. </

    div

    >
  29. </

    div

    >
  30. <div

    class

    =

    "col-md-6"

    style

    =

    "border:1px solid #000; height:200px; padding-top:10%; padding-left:10%;"

    ondrop

    =

    "drop(event)"

    ondragover

    =

    "dragOver(event)"

    >
  31. <h2

    >

    DRAG HERE</

    h2

    >
  32. </

    div

    >

  33. </

    div

    >
  34. <script

    src

    =

    "script.js"

    ></

    script

    >
  35. </

    body

    >
  36. </

    html

    >

Creating JavaScript Function

This is where the main function of the application is. This code will let you dynamically change a background color. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. function

    drop(

    e)

    {
  2. e.preventDefault

    (

    )

    ;
  3. let data =

    e.dataTransfer

    .getData

    (

    "data"

    )

    ;
  4. e.target

    .style

    .backgroundColor

    =

    data;
  5. e.target

    .innerHTML

    =

    ""

    ;

  6. }

  7. function

    drag(

    e)

    {
  8. e.dataTransfer

    .setData

    (

    "data"

    ,

    e.target

    .id

    )

    ;
  9. }

  10. function

    dragOver(

    e)

    {
  11. e.preventDefault

    (

    )

    ;
  12. }

In this code we will create first a method that will detect the appended object called drop()function, this will detect the dropped object and will get the data id. After we getting the object id we will apply the data as the background color of the targeted element. Next we will create a method called drag(), this method will set an object data that will will be carry over while dragging. Lastly we will create a method called called dragOver() function, this will allow the object to be draggable.

Output:

Drag%20and%20Drop%20Change%20Background%20Color%20in%20JavaScript%201.png


The Drag and Drop Change Background Color in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created Drag and Drop Change Background Color 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,496

342,218

342,226

Top