Curriculum
Course: HTML Basic
Login

Curriculum

HTML Basic

HTML Introduction

0/1

HTML Editors

0/1

HTML Attributes

0/1

HTML Paragraphs

0/1

HTML Formatting

0/1

HTML Comments

0/1

HTML Favicon

0/1

HTML Page Title

0/1

HTML Iframes

0/1

HTML Java Script

0/1

HTML File Paths

0/1

HTML Symbols

0/1
Text lesson

Where to Drop – ondragover

The ondragover event dictates where the dragged data can be dropped.

By default, elements do not accept drops from other elements. To enable dropping, we need to prevent the default behavior of the element.

This is achieved by invoking the event.preventDefault() method within the ondragover event handler:

event.preventDefault()

Do the Drop – ondrop

When the dragged data is dropped, a drop event is triggered.

In the example provided, the ondrop attribute invokes a function named drop(event):

function drop(ev) {
  ev.preventDefault();
  var data = ev.dataTransfer.getData(“text”);
  ev.target.appendChild(document.getElementById(data));
}

Here’s the code explained:

  1. Use preventDefault() to stop the browser from handling the data with its default action (typically opening as a link on drop).
  2. Retrieve the dragged data using dataTransfer.getData() method, which returns data set with the same type in the setData() method.
  3. The dragged data is the id of the dragged element (“drag1”).
  4. Append the dragged element into the drop element