mirror of
https://github.com/lospec/pixel-editor.git
synced 2023-08-10 21:12:51 +03:00
22 lines
771 B
JavaScript
22 lines
771 B
JavaScript
|
//add event listener for any element which calls a function
|
||
|
//element can be provided as a direct reference or with just a string of the name
|
||
|
|
||
|
function on(event, elementId, functionCallback) {
|
||
|
|
||
|
|
||
|
|
||
|
//if element provided is string, get the actual element
|
||
|
var element = (typeof elementId == 'string' ? document.getElementById(elementId) : elementId);
|
||
|
|
||
|
//console.log('added '+event+' event listener on '+element)
|
||
|
|
||
|
element.addEventListener(event,
|
||
|
function (e) {
|
||
|
// e = event
|
||
|
//this = element clicked
|
||
|
functionCallback(e, this);
|
||
|
//if you need to access the event or this variable, you need to add them
|
||
|
//when you define the callback, but you cant use the word this, eg:
|
||
|
//on('click', menuButton, function (e, button) {});
|
||
|
});
|
||
|
}
|