A no-frills cross-browser DOM event listener with ender integration.
- 0.6kb gzipped (1.2kb minified)
- Cross-browser event listeners (IE6+, and everybody else)
- Cross-browser
preventDefault
andstopPropagation
- Event delegation with or without a selector engine
- Ender bridge with wrapper prototype methods
Install with npm
$ npm install listener
... or build with ender
$ ender build listener
... or just download and include like any other script.
Please see full API docs down the page
listener(params, handler)
listenerObj.attach()
listenerObj.detach()
listenerObj.fire([arg1, [argN]])
listener.preventDefault(event)
listener.stopPropagation(event)
Ender bridge
// ender methods
ender.listener(params, handler)
ender.preventDefault(event)
ender.stopPropagation(event)
// ender element wrapper prototype methods
wrap.attach(event, handler)
wrap.detach(namespace)
wrap.fire(namespace)
wrap.delegate(event, delegation, handler)
When including a selector engine, include it before listener
, and then you can delegate events with a selector supported by the engine. Otherwise you can delegate with functions or a single tag selector.
The ender API is included at the end of the docs.
The tests require qunit, it's a submodule so do this before running:
$ git submodule init
$ git submodule update
And then you can open the tests/index.html
file in your browser.
(c) Ryan Florence 2011, MIT License
Listener can be built with ender, see the "Ender Bridge Documentation" at the end.
Creates a new event listener object.
Note: When using a string for delegate
you should define a selector engine with listener._select = function(){...}
. If you're building with ender, build the selector engine (like qwery) before listener. The Ender API bridge assigns the listener's engine to ender's, if it's undefined, you're hosed.
listener(params, handler)
-
params
(object) - Key value pairs of parameters for the listener.-
node
(element) - A DOM element to attach the listener to. -
event
(string) - Which event to listen to, i.e. 'click'. -
capture
(boolean: defaultfalse
) - Initiates capture. W3 Reference -
delegate
(mixed) - Accepts a CSS selector or a functionSignature
function (node)
Arguments
node
(element) - The element the event is listening on
Returns Array - You must return an element collection, or array of elements you want to be matched against the event target.
-
-
handler
(function) - The event handler.
Object - A listener object.
A basic click listener
var params = {
node: document.getElementById('foo'),
event: 'click'
}
listener(params, function (){
alert('hey!')
})
Delegate with function
var params = {
node: document.getElementById('foo'),
event: 'click',
delegate: function (node){
// return a collection of elements
// only buttons will fire the handler
return node.getElementsByTagName('button')
}
}
listener(params, function (){
alert('hey!')
})
Delegate with CSS selector
listener({
node: document.getElementById('foo'),
event: 'click',
delegate: 'button' // same as above but uses the selector engine
}, function (){
alert('hey!')
})
Attaches the listener to the element.
listenerObj.attach()
undefined
var listenerObj = listener({
node: document.getElementById('foo'),
event: 'click'
}, function (){
alert('hey!')
})
listenerObj.detach()
// click does nothing
listenerObj.attach()
// click is attached again
Detaches the listener from the element.
listenerObj.detach()
undefined
var listenerObj = listener({
node: document.getElementById('foo'),
event: 'click'
}, function (){
alert('hey!')
})
listenerObj.detach()
// click does nothing
listenerObj.attach()
// click is attached again
Fires the handler of a listener object.
listener.fire([arg1, [argN]])
arg1
(mixed: optional) - An argument to be passed to the handler, often a mock event object should be supplied.argN
(mixed: optional) - Any number of arguments passed in will be applied to the handler.
undefined
var listenerObj = listener({
node: document.getElementById('foo'),
event: 'keyup'
}, function (event){
event.keyCode //> 'fake'
})
listenerObj.fire({keyCode: 'fake'})
Cross-browser method to cancel an event without stopping further propagation of the event.
listener.preventDefault(event)
event
(event) - A DOM event object
undefined
listener({
event: 'click',
node: document.getElementById('foo')
}, function (event){
listener.preventDefault(event) // cancels event
})
Cross-browser method to prevent bubbling (propagation) of an event.
listener.stopPropagation(event)
event
(event) - A DOM event object
undefined
listener({
event: 'click',
node: document.getElementById('foo')
}, function (event){
listener.stopPropagation(event) // prevents bubbling
})
When building with ender the listener
function is returned to its previous definition and then added to the ender client-side API.
Identical to listener
, see docs above.
Identical to listener.preventDefault
, see docs above.
Identical to listener.stopPropagation
, see docs above.
Attaches an event handler to each element in the wrap.
wrap.attach(event, handler)
event
(string) - The event handler to add, i.e. 'click'handler
(function) - The event handler.
Object - the wrap
// assumes you've got a selector engine
var wrap = $('#some-el')
wrap.attach('click', function (){
// do something
})
Detaches any event handlers added with a namespace from each element in the wrap.
wrap.detach(namespace)
namespace
(string) - The namespace of events.
Object - the wrap
// assumes you've got a selector engine
var wrap = $('#some-el')
wrap.attach('click.foo', function (event){
// do something
})
wrap.attach('mouseover.foo', function (event){
// do something else
})
// later
wrap.detach('foo') // removes both listeners
Fires any event handlers added with a namespace for each element in the wrap.
wrap.fire(namespace)
namespace
(string) - The namespace of events.
Object - wrap
// assumes you've got a selector engine
var wrap = $('#some-el')
wrap.attach('click.foo', function (event){
// do something
})
wrap.attach('mouseover.foo', function (event){
// do something else
})
wrap.fire('foo') // fires both listeners
Delegates an event for each element in the wrapper.
wrap.delegate(event, delegation, handler)
-
event
(string) - The event handler to add, i.e. 'click' -
delegation
(mixed) - Accepts a CSS selector or a function.Signature
function (node)
Arguments
node
(element) - The element the event is listening on
Returns Array - You must return an element collection, or array of elements you want to be matched against the event target.
-
handler
(function) - The event handler.
Object - wrap
delegate with selector
// assumes you've got a selector engine
var wrap = $('#some-el')
wrap.delegate('click', 'a', function (event, target){
// `this` and `target` are the clicked anchor tag
})
delegate with function
var delegation = function (node){
return node.getElementsByTagName('foo')
}
// assumes you've got a selector engine
var wrap = $('#some-el')
wrap.delegate('click', delegation, function (event, target){
// do stuff with the target
})