The Wayback Machine - https://web.archive.org/web/20240930170149/https://www.geeksforgeeks.org/html-ondrag-event-attribute/
Open In App

HTML ondrag Event Attribute

Last Updated : 11 Dec, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The ondrag event attribute triggers when the element or text selection is being dragged in HTML. This event is very similar to the drag-and-drop event. This will triggered during a drag operation when an HTML element is being dragged.

Syntax

<element ondrag = "script">

Supported Tags

It supports all HTML Elements. 

Attribute Value

This attribute contains a single value script that works when an element is dragged. It is supported by all HTML elements.

Example: This example illustrates the implementation of the HTML ondrag Event Attribute.

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>ondrag event attribute</title>
    <style>
        body {
            text-align: center;
        }
 
        #geeks {
            border: 1px solid black;
            padding: 15px;
            width: 60%;
        }
    </style>
</head>
 
<body>
    <h1 style="color:green">
        GeeksforGeeks
    </h1>
    <h2>
        ondrag event attribute
    </h2>
    <div id="geeks"
         draggable="true"
         ondrag="Function()">
        GeeksforGeeks: A computer
        science portal for geeks
    </div>
 
    <script>
        function Function() {
            document.getElementById("geeks").
                style.fontSize = "30px";
            document.getElementById("geeks").
                style.color = "green";
        }
    </script>
</body>
 
</html>


Output:

Supported Browsers

The browser supported by ondrag event attribute are listed below: 

  • Google Chrome 3 and above
  • Edge 12 and above
  • Firefox 3.5 and above
  • Opera 12 and above
  • Safari 3.1 and above


Previous Article
Next Article

Similar Reads

script.aculo.us Drag & Drop onDrag Option
The script.aculo.us library is a cross-browser library that aims at improving the user interface of a website. The Drag & Drop module can be used to make any element drag-gable and also enables them to be dropped in a drop zone. The onDrag option is used to specify a callback function that would be invoked whenever the element is currently bein
2 min read
jQWidgets jqxDragDrop onDrag Property
jQWidgets is a JavaScript framework for making web-based applications for PC and mobile devices. It is a very powerful, optimized, platform-independent, and widely supported framework. The jqxDragDrop is used to represent a jQuery drag and drop widget that is used to make any DOM element draggable. It can be used in combination with many widgets li
2 min read
HTML onsearch Event Attribute
This onsearch attribute works when a user presses the ENTER button or clicks on the x button. This event works with <input> element. Supported Tags: <input type="search">Syntax: <element onsearch="script">Attribute: This event contains single attribute script which contains the script to be run when event call. Example: C/C++ Code
1 min read
HTML oninput Event Attribute
This attribute works when it gets user input value. This attribute mainly fires when the user change the value of <input> and <textarea> element. This attribute is quite similar to onchange attribute but the basic difference is that the oninput event attribute occurs immediately when the value of the element changes while the onchange a
1 min read
HTML | onmouseup Event Attribute
This attribute fires when a mouse button is released over the element. The order of events occur related to the onmouseup event. onmousedownonmouseuponclick Supported Tags: All HTML elements, EXCEPT: <base> <bdo><br><head><html><iframe><meta><param><script><style> <title> Syntax:
1 min read
HTML | onwheel Event Attribute
This attribute works when the wheel of pointer device is rolled up or down over an element. The onwheel attribute also works when the user scrolls or zooms on an element by using mouse or touchpad. Supported Tags: It supports all HTML elements. Syntax: <element onwheel = "script"> Attribute Value: This attribute contains single value script a
1 min read
HTML | oncontextmenu Event Attribute
This attribute works when the user right-clicks on an element to open the context menu.Supported Tags: It supports all HTML elements. Syntax: <element oncontextmenu="script"> Attribute Value: This attribute contains single value script. The script to be run when oncontextmenu attribute called. It is supported by all HTML elements.Note: This a
1 min read
HTML | ononline Event Attribute
The ononline event attribute works when the browser starts working in online mode. it is opposite to onoffline event attribute.Supported Tags <body> syntax <element ononline = "script"> Attribute Value: This event attribute contains single value script which works when browser working in online mode.Example: C/C++ Code <!DOCTYPE HTML
1 min read
HTML | ondragover Event Attribute
The ondragover event attribute will trigger if a draggable element or text is being dragged to a valid drop target. To drag an element garb that element and drag it to the drag point. Here we will use the global HTML 5 draggable attribute. The data and elements can't be dropped. To allow a drop, you have to call the event.preventDefault() method. I
2 min read
HTML onended Event Attribute
The HTML onended Attribute is a event attribute which works when the audio/video is ended. We can add some custom message in this event like “Thank for watching”, “Share”, etc. Usage: This attribute is used in <audio> and <video> element. Syntax: <element onended="myScript">Attribute Value: This attribute contains single value scr
1 min read
HTML onmouseover Event Attribute
The onmouseover event attribute works when the mouse pointer moves over the specified element. It is basically: Activated when the mouse pointer enters the designated HTML element.Enables executing JavaScript code when the mouse hovers over the element.Ideal for enhancing user experience by providing feedback or additional information.Syntax<ele
2 min read
HTML onunload Event Attribute
The onunload event attribute works when the document is being unloaded i.e. it occurs when the browser has been closed. It is mostly used when the user opens a link and submits the form and closes the browser window. Basically, it is: Fired when a user navigates away from a page or closes the browser.Useful for executing cleanup actions or displayi
1 min read
HTML ontoggle Event Attribute
The ontoggle event is triggered when the user opens or closes the <details> element. The <details> element is used to provide additional information/details that the user can view or hide details according to requirements. Syntax<details ontoggle = "script">Supported Tag<details>: This attribute works with a <details>
2 min read
HTML onhashchange Event Attribute
HTML onhashchange Event Attribute works when there have been changes to the anchor part. The anchor part starts with the '#' symbol of the current URL. It also enables the execution of a script or function on hash change and is used for dynamic web applications updating without full page reload. In order to trigger this event, we have a couple of o
2 min read
HTML ondragstart Event Attribute
HTML ondragstart Event Attribute is used when the user wants to drag the text or element. It is simply the process in which we press on the desired text to drag and drop them to a different location. Basically, it Initiates when the user starts dragging an element and is used to set data to be transferred or apply visual effects during drag start.
3 min read
HTML ondragenter Event Attribute
The ondragenter Event attribute in HTML works when the content is draggable. An element is made by setting the draggable attribute value to true. The draggable attribute can take only true or false values and activates when a dragged element enters a valid drop target. Syntax<element ondragenter = "script">Attribute ValueThis attribute contai
2 min read
HTML onbeforeunload Event Attribute
The onbeforeunload event runs when the document is about to be unloaded, thus, allowing to display message in a dialog box to inform the user whether a user wants to stay or leave the current page, i.e., this event triggers just before a user leaves a webpage, and helps to prevent an accidental closures and retaining unsaved changes. The message re
1 min read
HTML onpaste Event Attribute
The onpaste attribute works when some content is pasted in an element. This event attribute is supported by all HTML elements. It is mostly used with <input> element.There are three ways to paste the content in HTML elements which are listed below:  Use CTRL + V keySelect "Paste" from the edit menu in the browser.select the "Paste" command fr
2 min read
HTML onselect Event Attribute
The onselect event attribute works when some text has been selected in an element. It is the part of event attribute. It is basically activated when text within an input or textarea is selected and enhances user interaction by responding to text highlighting. Syntax: <element onselect = "script">Example 1: In this example, we will see the imp
2 min read
HTML ondragend Event Attribute
The ondragend event attribute works when the user finishes the dragging of an element. The drag-and-drop feature is common in HTML5. Any element can be made draggable by using the HTML5 draggable attribute. It is fired when the user releases the mouse button or touch input also used for integrating custom behaviors like updating the interface or tr
3 min read
HTML onsubmit Event Attribute
The onsubmit event attribute in HTML is triggered when a form is submitted. It is basically triggered when a form is submitted, either by clicking a submit button or using JavaScript. It is also useful for validating form data or performing actions before any submission and ensures better control and validation of user inputs before data is sent. S
2 min read
HTML onpageshow Event Attribute
The onpageshow event occurs when a user navigates to a website. This event is quite similar to onload event but it occurs after the onload event. It occurs every time, when the page is loaded whereas the onload event does not occur when the page is loaded from the cache. When a page is loaded or restored, it triggers and provides session history in
2 min read
HTML onchange Event Attribute
The onchange event attribute works when the value of the element changes and select the new value from the List. It is a part of the event attribute. It is similar to oninput event attribute. But the difference is that oninput attribute event occurs immediately after the value of the element changes, while onchange event occurs when the element los
2 min read
HTML ondragleave Event Attribute
The ondragleave attribute works when a draggable element or text selection leaves a valid drop target. It helps in dragging the elements and get to know the entering or leaving a drop target. Drag and drop feature is very popular in HTML5. Use CSS property when the element is draggable and enter into the drop target. It is fired when an object bein
3 min read
HTML onmouseout Event Attribute
The HTML  onmouseout event attribute works when the mouse pointer moves out of the specified element. It activates when the mouse pointer moves out of an HTML element. When a user moves the mouse away, this can be utilized to activate specific actions or functions and enables dynamic changes in response to mouse interactions, enhancing user experie
2 min read
HTML ondblclick Event Attribute
The ondblclick Attribute Event occurs when a user fires the mouse Double click on Element. It is triggered when a user double-clicks on an HTML element and is used to initiate actions or functions in response to a double-click event. It also enables interactive features like opening pop-ups, displaying details, or toggling elements. Syntax: <ele
2 min read
HTML onkeydown Event Attribute
The onkeydown event attribute works when the user presses any key from the keyboard. The onkeydown event is triggered when the down key is pressed down and used for capturing and responding to key events. Syntax: <element onkeydown = "script">Supported Tags: It supports all HTML elements EXCEPT-  <base> <bdo> <br> <head
2 min read
HTML onreset Event Attribute
The onreset event attribute in HTML is triggered when reset the form. This Attribute works with form tag and is triggered when a form is reset, either by user action or programmatically. It also allows the execution of scripts or functions to handle actions after form reset. Syntax:<form onreset = "script">Supported Tags: <form>Attribut
2 min read
HTML onload Event Attribute
The onload event attribute works when an object has been loaded. This attribute is mostly used within the <body> element to execute a script. It can be used with other elements as well. This attribute is used to check the visitor's browser type and browser version and load the proper version of the web page based on the information. In simple
2 min read
HTML onerror Event Attribute
The onerror event works when an error occurs while loading an external file. The external file may contain a document file or an image file. It simply activates when an error occurs during the loading of an external resource and It is commonly used to handle issues with image loading, script execution, or other external file errors. Syntax: <ele
2 min read