Javascript Course


HTML5 includes new attributes that can be used for Drag and Drop effects.
Here are the HTML5 attributes you can use to create drag and drop effects:


- Syntax to use these 'on...' attributes in the HTML tag for drag-and-drop effects:
attribute_name='handler(event)'
- handler(event) - is a function that can be called when the specified event is fired.

Syntax to create a draggable element:

To make an element draggable, set the draggable attribute to 'true', and add also the ondragstart attribute.
<tag draggable='true' ondragstart='handler(event)' id='draggable_elm'>Content</tag>

Syntax to create a drop-target element:

By default, the target of a drag operation can be an editable element (textarea, input:text, input:password, input:search), an element in content editable mode, or a document in design mode. If you want to allow another elements to be a drop target, add the ondrop attribute, and cancel the ondragover event (using the value: 'return false', or a function with event.preventDefault()). To be more sure, cancel also the ondragenter event.
<tag id='target_drop' ondrop='someFunction(event)' ondragenter='return false;' ondragover='allowDrop(event)'>Drop here</tag>

Examples Drag and Drop

1. Dragging and Dropping a Div

A DIV with some content dragged and dropped in another DIV.
<!doctype html>
<html>
<head>
<title>title</title>
<style>
#drag1 {
 width: 12em;
 margin: .2em;
 background-color: #f0f0b0;
 border: 2px dashed #000;
 padding: .2em .5em;
 color: #0001da;
 cursor: pointer;
}
#target_drop1 {
 width: 20em;
 min-height: 5em;
 margin: 1em;
 background-color: #ededfe;
 padding: .2em .5em;
 border: 2px solid #000;
}
</style>
</head>
<body>
<h4>Example Drag and Drop HTML5 - JavaScript</h4>

<div draggable='true' ondragstart='dragStart(event)' id='drag1'>Drag this.<br/>http://www.coursesweb.net/</div>
<div id='target_drop1' ondrop='drop(event)' ondragenter='return false;' ondragover='allowDrop(event)'>Drop here</div>

<script>
function allowDrop(ev) {
 ev.preventDefault(); // cancel the ev event
}

// function called when the drag operation starts
function dragStart(ev) {
 // sets the data type and the value of the dragged data
 // This data will be returned by getData(). Here the ID of current dragged element 
 ev.dataTransfer.setData('Text', ev.target.id);
}

// function called when the dragged element is dropped
function drop(ev) {
 ev.preventDefault(); // the dragover event needs to be canceled to allow firing the drop event

 // gets data set by setData() for current drag-and-drop operation (the ID of current dragged element),
 // using for parameter a string with the same format set in setData
 var drag_data = ev.dataTransfer.getData('Text');

 // adds /appends the dropped element in the specified target
 ev.target.appendChild(document.getElementById(drag_data));
}
</script>
</body>
</html>

Explanations of the code above

• In the draggable element, is set: draggable='true', then, the ondragstart='dragStart(event)' specifies what should happen when the users starts dragging the element, calls the dragStart() function that specifies what data to be dragged.
In this function, the code: ev.dataTransfer.setData('Text', ev.target.id); sets the data type (in this case 'Text') and the value of the dragged data (the ID of the draggable element).

• In the other DIV, ondragover event specifies where the dragged data can be dropped. Because data/elements cannot be dropped by default in DIV tag, we call a function, allowDrop() that uses the event.preventDefault() method to prevent the default handling of the element.
We use also ondragenter='return false;' to prevent the default handling of the element when it enters in the drop target area.
- With: ondrop='drop(event)' we specify what to happen when the data is dropped, calls the drop() function which contains this code:
function drop(ev) {
 ev.preventDefault();
 var drag_data = ev.dataTransfer.getData('Text');
 ev.target.appendChild(document.getElementById(drag_data));
}
- preventDefault() to prevent the browser default handling of the data (default is open as link in browser on drop).
- With the ev.dataTransfer.getData('Text') method we get the dragged data. This method will return any data that was set to the same type ('Text') in the setData() method (in this case, the ID of the dragged element).
- Then, ev.target.appendChild(document.getElementById(drag_data)); appends the dragged element into the drop-target element.

2. Dragging and Dropping an Image

Drag and drop an image back and forth between two <div>, by making both DIVs a drop-target element, with the 'ondragover' and 'ondrop' attributes.
<!doctype html>
<html>
<head>
<title>title</title>
<style>
#div1, #div2 {
float: left;
width: 12em;
min-height: 75px;
margin: .2em;
border: 2px solid #000;
padding: .2em;
}
#div2 {
background:#bbfbbb;
}
.todrag {
cursor: pointer;
}
.dragging {
border: 1px dashed #00f;
background: #ff1;
}
</style>
</head>
<body>
<h4>Example Drag and Drop image - HTML5 - JavaScript</h4>
<p>Click, drag and drop the image from one rectangle to another and back.</p>

<div id='div1' ondrop='drop(event)' ondragover='allowDrop(event)'>
 <img src='../imgs/webcourses.gif' draggable='true' ondragstart='dragStart(event)' id='drag2' class='todrag' width='191' height='63' alt='Drag-and-Drop image' />
</div>
<div id='div2' ondrop='drop(event)' ondragover='allowDrop(event)'></div>
<br style='clear:both' />
<script>
function allowDrop(ev) {
 ev.preventDefault(); // cancel the ev event
}

// function called when the drag operation starts
function dragStart(ev) {
 // sets the data type and the value of the dragged data
 // This data will be returned by getData(). Here the ID of current dragged element 
 ev.dataTransfer.setData('Text', ev.target.id);

 // sets another css class
 ev.target.className = 'dragging';
}

// function called when the dragged element is dropped
function drop(ev) {
 ev.preventDefault(); // the dragover event needs to be canceled to allow firing the drop event

 // gets data set by setData() for current drag-and-drop operation (the ID of current dragged element),
 // using for parameter a string with the same format set in setData
 var drag_data = ev.dataTransfer.getData('Text');

 // adds /appends the dropped element in the drop-target
 ev.target.appendChild(document.getElementById(drag_data));

 // sets another css class
 document.getElementById(drag_data).className = 'todrag';
}
</script>
</body>
</html>

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
Drag and Drop with HTML5 attributes

Last accessed pages

  1. PHP Simple HTML DOM Parser (12452)
  2. Disable button and Enable it after specified time (17532)
  3. Get Mime Type of file or string content in PHP (6229)
  4. PHP MySQL - using MySQLi (9669)
  5. Integer and Float value in Select with PDO from string to numeric (8658)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (477)
  2. CSS cursor property - Custom Cursors (81)
  3. The Mastery of Love (71)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (63)
  5. CSS3 2D transforms (46)