Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Rok
    Rok is offline
    New Coder
    Join Date
    Sep 2002
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to addEventListener?

    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:

    document.getElementsByTagName('img')[0].addEventListener
    but don't know the rest.

    How to solve this?

  • #2
    Rok
    Rok is offline
    New Coder
    Join Date
    Sep 2002
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I just found out. IE doesn't support addEventListener but uses attachEvent instead.

  • #3
    Regular Coder
    Join Date
    May 2002
    Location
    Helsinki, Finland
    Posts
    231
    Thanks
    0
    Thanked 1 Time in 1 Post
    You can also use syntax (works on IE & Moz) :
    oObject.onEvent = sFunction;

    an example :
    Code:
    <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?!?");
    }
    // -->
    Zvona
    First Aid for
    Web Design

  • #4
    Rok
    Rok is offline
    New Coder
    Join Date
    Sep 2002
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •