Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ondragstart: convert static example to live example #15318

Merged
merged 4 commits into from
Apr 29, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 96 additions & 43 deletions files/en-us/web/api/globaleventhandlers/ondragstart/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ A {{domxref("GlobalEventHandlers","global event handler")}} for the
## Syntax

```js
var dragstartHandler = targetElement.ondragstart;
targetElement.ondragstart = dragstartHandler;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @AnilSeervi but I would literally copy the structure, using value rather than return type - so it would look like this

image

```

### Return value
AnilSeervi marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -27,63 +27,116 @@ var dragstartHandler = targetElement.ondragstart;
## Example

This example demonstrates using the
{{domxref("GlobalEventHandlers.ondragstart","ondragstart")}} attribute handler to set an
element's {{event("dragstart")}} event handler.
{{domxref("GlobalEventHandlers.ondragstart","ondragstart")}} global event handler to set an
element's `dragstart` event handler.

```js
<!DOCTYPE html>
<html lang=en>
<title>Examples of using the ondrag Global Event Attribute</title>
<meta content="width=device-width">
<style>
div {
margin: 0em;
### HTML

```html
<div>
<p id="source" draggable="true">
Select this element, drag it to the Drop Zone and then release the selection to move the element.
</p>
</div>
<div id="target">Drop Zone</div>
Copy link
Contributor

@OnkarRuikar OnkarRuikar Apr 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<div id="target">Drop Zone</div>
<div id="destination">Drop Zone</div>

As we are on an event page, the 'target' and 'event.target' can become source of confusion.
Also, in syntax section we used target.ondragstart but in code we say source.ondragstart. Are we calling draggable elements 'target' or 'source'? In the code we can not use target.ondragstart cos we are calling destination "target" here.


<textarea readonly id="event-log"></textarea>
<button id="reload">Reload</button>
```

### CSS

```css
div, #event-log {
margin: 1em;
}

#source, #target {
padding: 2em;
}
#source {
color: blue;
border: 1px solid black;
}
#target {
border: 1px solid black;
}
</style>
</head>
<script>
function drag_handler(ev) {
console.log("Drag");
}

#source {
color: blue;
}

#event-log {
width: 25rem;
height: 4rem;
AnilSeervi marked this conversation as resolved.
Show resolved Hide resolved
margin-bottom: 0;
padding: .2rem;
}
```

### JavaScript

```js
const source = document.getElementById("source");
const target = document.getElementById("target");
const event_log = document.getElementById("event-log");

function dragstart_handler(ev) {
console.log("dragStart");
ev.dataTransfer.setData("text", ev.target.id);
event_log.textContent += "dragStart\n";
// Change the source element's background color to signify drag has started
ev.currentTarget.style.border = "dashed";
ev.dataTransfer.setData("text", ev.target.id);
}

function dragover_handler(ev) {
event_log.textContent += "dragOver\n";
// Change the target element's border to signify a drag over event
// has occurred
ev.currentTarget.style.background = "lightblue";
ev.preventDefault();
}

function drop_handler(ev) {
console.log("Drop");
ev.currentTarget.style.background = "lightyellow";
event_log.textContent += "Drop\n";
AnilSeervi marked this conversation as resolved.
Show resolved Hide resolved
ev.preventDefault();
const data = ev.dataTransfer.getData("text");
ev.target.appendChild(document.getElementById(data));
}

ev.preventDefault();
var data = ev.dataTransfer.getData("text");
ev.target.appendChild(document.getElementById(data));
function dragenter_handler(ev) {
event_log.textContent += "dragEnter\n";
// Change the source element's background color for enter events
ev.currentTarget.style.background = "yellow";
}

function dragover_handler(ev) {
console.log("dragOver");
ev.preventDefault();
function dragleave_handler(ev) {
event_log.textContent += "dragLeave\n";
// Change the source element's border back to white
ev.currentTarget.style.background = "white";
}

function dragend_handler(ev) {
event_log.textContent += "dragEnd\n";
// Change the target element's background color to visually indicate
// the drag ended.
target.style.background = "pink";
}
</script>
<body>
<h1>Examples of <code>ondrag</code>, <code>ondrop</code>, <code>ondragstart</code>, <code>ondragover</code></h1>
<div>
<p id="source" ondrag="drag_handler(event);" ondragstart="dragstart_handler(event);" draggable="true">
Select this element, drag it to the Drop Zone and then release the selection to move the element.</p>
</div>
<div id="target" ondrop="drop_handler(event);" ondragover="dragover_handler(event);">Drop Zone</div>
</body>
</html>

// Set handlers for the source's drag - start/enter/leave/end events
source.ondragstart = dragstart_handler;
source.ondragenter = dragenter_handler;
Copy link
Contributor

@OnkarRuikar OnkarRuikar Apr 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
source.ondragenter = dragenter_handler;
target.ondragenter = dragenter_handler;

I think these(also ondragleave) should be registered on drop target[ref]. On drag leave the target element is not resetting to white color from lightblue. There is no need to indicate that the draggable is on source element itself.

source.ondragleave = dragleave_handler;
source.ondragend = dragend_handler;

// Set handlers for the target's drop and dragover events
target.ondrop = drop_handler;
target.ondragover = dragover_handler;

// Set click event listener on button to reload the example
const button = document.getElementById("reload");
button.addEventListener("click", () => {
document.location.reload();
});
```

### Result

{{ EmbedLiveSample('Example', '100', '450') }}

## Specifications

{{Specifications}}
Expand Down