By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,451 Members | 981 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,451 IT Pros & Developers. It's quick & easy.

addEventListener and bind

P: n/a
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/metho...nces.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
Nov 27 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
VK

Andrew Poulos wrote:
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/metho...nces.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>

Nov 27 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.