|
If you find this helpful, please click the Google |
HTML Event Handlers
HTML Attributes for Event Handlers
Drag and drop the item names below:
| $ Buy Now $ | Save for Later |
|---|---|
|
|
This is an actual working demo of the drag and drop example code below. (Do View Source to verify that this page is using the HTML 5 DOCTYPE. You can also verify it is Valid HTML 5 using the HTML Validator. Try using it to validate URLs with HTML examples from other places that claim to be HTML 5 web sites!)
HTML External Resource Events
onloadonunloadonabort- code to be executed when the document loading is cancelled, possibly when the user stops the page load or navigates to another page before loading has completed
HTML Window Events for on the <body> tag
onbeforeunloadonhashchangeonmessageonofflineononlineonpagehideonpageshowonpopstateonredoonresizeonstorageonundoonunload
HTML Keyboard Events
onkeypressonkeydownonkeyup
HTML Mouse Events
onmouseover- An
onmouseoverevent is fired on an element when the cursor is moved over the element. Theonmouseoverandonmouseoutevents are often used to create rollover effects. onmousemove- An
onmousemoveevent is fired on an element after theonmouseoverevent while the cursor is being moved around within the element. onmouseout- An
onmouseoutevent is fired on an element when the cursor is moved off of an element. Theonmouseoverandonmouseoutevents are often used to create rollover effects. onclick- An
onclickevent is fired on an element when the mouse has been single-clicked on that element. ondblclick- An
ondblclickevent is fired on an element when the mouse has been double-clicked on that element. onmousedown- An
onmousedownevent is fired on the element where the cursor is positioned when the mouse button has been pressed. onmouseup- An
onmouseupevent is fired on the element where the cursor is positioned when the mouse button has been released. onmousewheel- An
onmousewheelevent is fired on the element where the cursor is positioned when the user spins mouse wheel. ondragondragoverondragenterondragleaveondropondragend
HTML Form Events
onfocus- An
onblurevent is fired on an element when the element loses focus, such as when the user clicks the mouse or presses theTabkey to leave an input field. onblur- An
onblurevent is fired on an element when the element loses focus, such as when the user clicks the mouse or presses theTabkey to leave an input field. Theonblurevent is often used for validating input data. onchange- An
onchangeevent is fired on an element when the value of an element is different from its original value and it loses focus, such as when the user clicks the mouse or presses theTabkey to leave an input field. Theonchangeevent is often used for validating input data. - An
oncontextmenuevent is fired on an element just before the context menu for that element is to be displayed. onformchangeonforminputoninputoninvalidonselectonsubmitonreset
HTML Printer Events
onbeforeprintonafterprint
HTML Media Events
Although there are a number of attributes for handling media events defined as common attributes in the HTML 5 specification, they are only useful on the <audio> tag and the <video> tag. The documentation for the media event attributes are documented with those media elements.
Examples of HTML Event Handlers
Example of mouse events in HTML 5
<input type="submit" onmouseover="window.status='Submit form after filling in required fields'" onmouseout="window.status=''">
Example of drag and drop in HTML 5
(see drag and drop demo above)
On the draggable element(s), code the draggable and ondragstart attributes. On the drop location element(s), code the ondragenter, ondragover and ondrop attributes.
<table class="border">
<tr>
<th>$ Buy Now $</th>
<th>Save for Later</th>
</tr>
<tr style="vertical-align: top">
<td>
<ul style="list-style: none; width: 8em; height: 4em; margin: 0; padding: 0"
ondragenter="return true"
ondragover="return false"
ondrop="event.target.appendChild(document.getElementById(event.dataTransfer.getData('dragid')));
event.stopPropagation();
return false">
<li id="ex1cellphone" draggable="true"
style="margin: 0.8em; background-color: #999999; padding: 0 4px"
ondragstart="event.dataTransfer.effectAllowed='move';
event.dataTransfer.setData('dragid', event.target.getAttribute('id'));
return true">Cell Phone</li>
<li id="ex1flashdrive" draggable="true"
style="margin: 0.8em; background-color: #999999; padding: 0 4px"
ondragstart="event.dataTransfer.effectAllowed='move';
event.dataTransfer.setData('dragid', event.target.getAttribute('id'));
return true">Flash Drive</li>
</ul>
</td>
<td style="padding: 0">
<ul style="list-style: none; width: 8em; height: 4em; margin: 0; padding: 0 6px 20px 0"
ondragenter="return true" ondragover="return false"
ondrop="event.target.appendChild(document.getElementById(event.dataTransfer.getData('dragid')));
event.stopPropagation();
return false">
</ul>
</td>
</tr>
</table>