View Full Version : How to addEventListener?

09-07-2002, 11:06 PM
OK, lets make this easy :). I wont go into details of my function, I would like to first see it work :). So here's the thing:

I've read a small tutorial about addEventListener but it doesn't work for me. So what I want is this (well, that's not exactly what I want but I just want to know the concept first):

I have an image. I want when you click on it two alerts appear. Like this:
<img onclick="alert('a'); alert('b');">

But I don't want to do it thusly. I want to make it dynamically. So:
<img onclick="alert('a');">
and then something like:

but don't know the rest.

How to solve this?

09-07-2002, 11:13 PM
I just found out. IE doesn't support addEventListener but uses attachEvent instead.

09-08-2002, 01:21 PM
You can also use syntax (works on IE & Moz) :
oObject.onEvent = sFunction;

an example :

<script type="text/javascript">
var myImage = new Image();
var myImage2 = new Image();

myImage.onclick = zAlert;
myImage2.onmouseover = zOver;

function zOver(e)
alert("Never hover my images anymore!");

function zAlert(e)
alert("How dare you to click my images?!?");
// -->

09-08-2002, 04:01 PM
Yes I know about that but what I wanted to do is to dynamically set an additional event handler, which you can not do using javascript event handler properties.

Cause if you have
<img id="slika" onclick="alert('a');">
and then javascript:
document.getElementById('slika').onclick = function() {alert('b')}
and then click on the image, you would get only alert('b') but not both.

I know the solution now. Using addEventHandler or attachEvent (IE) you can accomplish this.