mirror of
https://github.com/lospec/pixel-editor.git
synced 2023-08-10 21:12:51 +03:00
first commit
This commit is contained in:
22
_ext/scripts/utilities/on.js
Normal file
22
_ext/scripts/utilities/on.js
Normal file
@ -0,0 +1,22 @@
|
||||
//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) {});
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user