SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    reminder:// attaching events

    How can I attach events with parameters?

    PHP Code:
    for (i=0i<document.getElementsByTagName('input').lengthi++)  document.getElementsByTagName('input')[i].attachEvent("onclick"element_onclick); // adds event to all input elements (saves time) 

  2. #2
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wait, so you want to know how to pass data to the function pointed at?
    Code:
    var arrI = document.getElementsByTagName('input');
    for (i=0; i < arrI.length; i++)
    	arrI[i].attachEvent("onclick", element_onclick);
    
    function element_onclick(param1, param2) {
    	// code n stuff
    	}
    Like param1 and param2?? What do you need to pass?
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  3. #3
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, basically I wanted to pass the object reference to the function and I cannot use

    arrI[i].attachEvent("onclick", element_onclick(param1, param2, etc));

    Now I can think of loads of work arounds to this such as the one I illustrated in this thread. However I cannot think of a way to add an event with a "parameter attached". Any idea's?

    PHP Code:
           function element_onclick()
           {
             
    // stub
             
    what window.event.srcElement// cheat :D
             // I could't remember how to attach a function with parameters, so using a work around for now
             // been a while since I done any javascript
             
    alert(what.value '\n' what.type '\n' what.parentElement.enctype); //just demo'ing:P
           


  4. #4
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For those that are listening in, andrew and I chatted last night and I came up with this solution:
    Code:
    var arrI = document.getElementsByTagName('input');
    for (i=0; i < arrI.length; i++)
    	arrI[i].attachEvent("onclick", function() {
    		element_onclick(arrI[i]);
    		});
    
    function element_onclick(elem) {
    	// code n stuff
    	}
    This way the element_onclick function receives a reference to the object that we have attached the onclick event to, not to mention that we could pass any other parameters we wanted.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  5. #5
    SitePoint Enthusiast
    Join Date
    Mar 2001
    Location
    northern Maine
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This code is not consistent. document.getElementById() is being used, implying some sort of desire for standard DOM methods. However attachEvent() is not defined anywhere in the specs, it is the addEventListener() method you are supposed to use:

    var arrI = document.getElementsByTagName('input');
    for (i=0; i < arrI.length; i++)
    arrI.item(i).addEventListener("click", function() {
    element_onclick(arrI[i]);
    }, false);

    If you are going to use attachEvent(), since you'd be writing IE-only code anyway, it makes more sense to use document.all and save a few bytes of characters.

    However, this is easily done correctly (and easy enough to hack around in IE), I would be deeply disappointed if you the proprietary code remained without equivalent DOM code.
    Jason - Contact Me
    Supermoderator @ CodingForums

  6. #6
    Currently Occupied; Till Sunda Andrew-J2000's Avatar
    Join Date
    Aug 2001
    Location
    London
    Posts
    2,475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just so you know, it was aimed specifically at internet explorer and was origionally from an old htc file. So it did not matter whether it was in-accordance to any standard DOM methods, simply because htc is not a standard.

  7. #7
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by jkd
    document.getElementById() is being used, implying some sort of desire for standard DOM methods
    I think that implication is empirical, and not as black and white as you seem to think it is, jkd.

    Just my opinion.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  8. #8
    SitePoint Guru momos's Avatar
    Join Date
    Apr 2004
    Location
    Belgium
    Posts
    919
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by beetle
    For those that are listening in, andrew and I chatted last night and I came up with this solution:
    Code:
    var arrI = document.getElementsByTagName('input');
    for (i=0; i < arrI.length; i++)
    	arrI[i].attachEvent("onclick", function() {
    		element_onclick(arrI[i]);
    		});
     
    function element_onclick(elem) {
    	// code n stuff
    	}
    This way the element_onclick function receives a reference to the object that we have attached the onclick event to, not to mention that we could pass any other parameters we wanted.
    this works if you want to attach the same event to a tag, but otherwise?

    Code:
     
    function init()
    {
    var winMenuLinks=document.getElementsByTagName('span');
    var i=0;
    while(i<winMenuLinks.length)
    {
    winMenuLinks[i++].attachEvent("onclick",function(){go( winMenuLinks[i].getAttribute('href') )});
    }
    }
    eg if my last span would have a link to google, they would all have a link to google...
    reason: the attach happens in the end. Solution?
    Last edited by momos; Jul 28, 2004 at 02:27.


Bookmarks

Posting Permissions

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