addEventListener and bind

A

Andrew Poulos

I've started moving my code from using from this format
obj.onclick = blah;
to this format
obj.addEventListener("click",blah,false);
There is a hitch though.

In some cases I've been using the 'bind' prototype idea from
<url: http://www.brockman.se/writing/method-references.html.utf8 >
to allow event listeners to be 'owned' by the object that triggered the
event.

Is there a way to combine addEventListener and the 'bind' idea?

Andrew Poulos
 
V

VK

Andrew said:
I've started moving my code from using from this format
obj.onclick = blah;
to this format
obj.addEventListener("click",blah,false);
There is a hitch though.

In some cases I've been using the 'bind' prototype idea from
<url: http://www.brockman.se/writing/method-references.html.utf8 >
to allow event listeners to be 'owned' by the object that triggered the
event.

Is there a way to combine addEventListener and the 'bind' idea?

I did not use the referenced material so I don't know of the my
"binding" idea is what you're looking for. Any way: here is some
inspiration (each new DIV has personal event listener and clone itself
onclick);

<html>
<head>
<title>TMP</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<script type="text/javascript">

function myDIV(c) {
var holder = c || document.body;
this.$_div = document.createElement('DIV');
this.$_div.innerHTML = 'Click me to clone';
with (this.$_div.style) {
width = '100px';
height = '50px';
backgroundColor = 'yellow';
borderColor = 'black';
borderStyle = 'solid';
marginTop = '10px';
marginRight = '10px';
marginBottom = '10px';
marginLeft = '10px';
paddingTop = '10px';
paddingRight = '10px';
paddingBottom = '10px';
paddingLeft = '10px';
cursor = 'pointer';
}

this.cloneIt = function(c) {
try {
holder.appendChild(new myDIV());
}
catch (e) {
/* resume next */
}
}

holder.appendChild(this.$_div);
if (this.$_div.addEventListener) {
this.$_div.addEventListener('click', this.cloneIt, false);
}
else if (this.$_div.attachEvent) {
this.$_div.attachEvent('onclick',this.cloneIt);
}
else {
// Some retard:
// don't bother with it.
}
}

function init() {
var d = new myDIV();
}
</script>

</head>

<body onload="init();">

</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top