This is my library that can handle events and get values from the event callbacks as if they were available everywhere. rather than just in the callback. It uses Higher Order Functions to handle all the displaying and grabbing of the values from the events
/*counter is a simple counter. it will add 1 to the count value every time count() is called. the display('querySelector string', 'attribute to assign the value') function returns a function that displays the count value on the element using a querySelector string and then assigns the count to the attribute name.
*/
var counter = function () {
var Counter = Object.create(null);
Counter.Count = 0;
Counter.count = function () {
Counter.Count++;
};
Counter.display = function (e, n) {
e = document.querySelector(e);
return (function () {
e[n] = Counter.Count;
return true;
});
};
return Counter;
};
/*eventGrabber can grab values from the event object. you specify these when initializing the object by passing the event object's name for these values as strings arguments ex:eventGrabber('pageX', 'pageY').
the names of theses values are stored in the first item in array
*/
var eventGrabber = function () {
var e = [].slice.call(arguments),
EventGrabber = Object.create(null);
EventGrabber.values = [];
e.forEach(function (E) {
EventGrabber.values.push(E);
EventGrabber[E] = 0;
});
EventGrabber.grab = function () {
return (function (eve) {
console.log(eve);
EventGrabber.values.forEach(function (v) {
EventGrabber[v] = eve[v];
});
});
};
EventGrabber.displayer = function (v, e, a) {
e = document.querySelector(e);
return function () {
e[a] = EventGrabber[v];
return true;
};
};
return EventGrabber;
};
var log = document.querySelector('#log');
/*constructors*/
var egrab = eventGrabber('pageX', 'pageY');
var testvar = counter();
/*the simple event listeners*/
window.addEventListener('mousemove', egrab.grab());
window.addEventListener('click', testvar.count);
window.addEventListener('click', function () {
var div =...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.