Skip to content

Instantly share code, notes, and snippets.

@nhunzaker
Created July 14, 2014 19:44
Show Gist options
  • Save nhunzaker/7bbafa9d82ec00106ed7 to your computer and use it in GitHub Desktop.
Save nhunzaker/7bbafa9d82ec00106ed7 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<title>Event runner</title>
<script src="http://modernizr.com/downloads/modernizr-latest.js"></script>
<style>
body {
font-family: sans-serif;
}
</style>
</head>
<body>
<form>
<input type="text" id="input" disabled />
<select id="select" disabled>
<option>Foo</option>
</select>
<textarea id="textarea" disabled="true" disabled>
Textarea
</textarea>
<button id="button" disabled></button>
</form>
<table>
<thead>
<tr>
<th>Element</th>
<th>Event</th>
</tr>
</thead>
<tbody id="triggered">
</tbody>
</table>
<script>
var mouseEvents = [
'click', 'contextmenu', 'DOMMouseScroll', 'dblclick',
'keydown', 'keypress', 'keyup', 'mousedown',
'mouseenter', 'mouseleave', 'mousemove', 'mouseout',
'mouseover', 'mouseup', 'mousewheel',
'pointerlockchange', 'pointerlockerror', 'wheel'
]
var focusEvents = ['focus', 'blur']
var touchEvents = [
'touchcancel', 'touchend', 'touchenter', 'touchleave',
'touchmove', 'touchstart'
];
var input = document.getElementById('input');
var triggered = document.getElementById('triggered');
function addEntry(name, event) {
var row = document.createElement('tr');
row.innerHTML = "<td>" + name + "</td><td>" + event + "</td>";
triggered.appendChild(row);
}
function test(pool, EventType, flag) {
var event;
for (var i = 0, len = pool.length; i < len; i++) {
event = pool[i];
input.addEventListener(event, function() {
addEntry('input', event);
}, false);
input.dispatchEvent(new EventType(event));
select.addEventListener(event, function() {
addEntry('select', event);
}, false);
select.dispatchEvent(new EventType(event));
textarea.addEventListener(event, function() {
addEntry('select', event);
}, false);
textarea.dispatchEvent(new EventType(event));
button.addEventListener(event, function() {
addEntry('button', event);
}, false);
button.dispatchEvent(new EventType(event));
}
}
test(mouseEvents, MouseEvent, 'mouse');
test(focusEvents, FocusEvent, 'focus');
if (Modernizr.touch) {
test(touchEvents, TouchEvent, 'touch');
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment