I have the below class, but have run into two problems:
I can only get it to work on existing elements, but I need to declare it for elements that later will be created dynamically. I believe a check on the eventlistener is needed, and perhaps using MutationObserver... and now I'm so far out of my depth...
If I do create two instances of the bar, for two elements that do exist, then when I mousedown or mouseup, they seem to target the same element. I believe this is because I'm using a prototype, and not this. But I can not get it to work if I use this.mousedown=func... instead.
var bar = (function(){
var me = this;
function bar(id){
me._id=id;
}
bar.prototype.mousedown=function(code){
el().addEventListener('mousedown', code, false);
return this;
}
bar.prototype.mouseup=function(code){
el().addEventListener('mouseup', code, false);
return this;
}
this.el=function(){
return document.getElementById(me._id);
};
this.css=function(){
return el().style;
};
return bar;
})();
Don't pay too much attention to the css() function and color changes, those are just for testing. The final code will hopefully have some ajax calls etc inside them instead.
function foo(s){
return new bar(s);
}
foo("batman")
.mousedown(
function(e){
css().background="green";
}
)
.mouseup(
function(e){
css().background="orange";
}
);