473,406 Members | 2,954 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

addEventListener and bind

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
1 3410
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: PJ | last post by:
Is it possible to extend the Node object so that the DOM function addEventListener can be used w/ IE? Does anyone have an example of this? Thanks, Paul
1
by: philjhanna | last post by:
Hi I'm having a problem applying return false to a div via addEventListener. I'm adding this so that I can drag (click-hold-move) over an image. (Its so that I can add zooming to the image)...
2
by: philjhanna | last post by:
Hi, Does anyone know why I can't add return false with addEventListener in firefox (1.0.6). This demonstrates the problem If return false is added to onmousedown then you can drag over the...
3
by: Jake Barnes | last post by:
37 Signals has built some awesome software with some features I wish I knew how to imitate. When I'm logged into my page (http://lkrubner.backpackit.com/pub/337271) any item that I mouseOver I'm...
10
by: Janus | last post by:
Hi, Is there a way to pass arguments to the callback function used inside an addEventListener? I see that I can only list the name of the callback function. For eg, I use this: var...
6
by: soulmach | last post by:
Using Firefox, I am trying to access a link within my HTML document, then use addEventListener() to add a click event to that link. I am using two external JS files. ajaxModal.js has the...
1
by: sylver | last post by:
Hi, Unregistering event listeners (or memory allocation) is a good practice in programming, but do we need to use removeEventListener() for all addEventListener() implementation? Please...
1
by: mebemikeyc | last post by:
With Element.addEventListener, you can easily wire-up several different event handlers on the fly. This works great, but debugging can be difficult if I don't know what exactly is reacting to an...
0
by: wpjoju | last post by:
i have this code which adds an event listener to a newly opened window but it doesn't seem to work in google chrome. the problem is window.addEventListener seem to work in chrome but if you do...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.