Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. dynamic event - pass value

dynamic event - pass value

Scheduled Pinned Locked Moved JavaScript
javascripttoolshelp
3 Posts 2 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hanzaplast
    wrote on last edited by
    #1

    hi guys. i have loop which needs to attach event on some elements. event must fire function and pass value from within loop. so here is problem... when loop is over all events fire same argument that is passed by ref not value at that time. <a href="#">link 1</a> <a href="#">link 2</a> <a href="#">link 3</a> <script type="text/javascript"> var anchorNiz = document.getElementsByTagName("a"); for(var i=0;i<anchorNiz.length;i++){ var anchor = anchorNiz[i]; anchor.attachEvent("onmouseover",function(){alert(i)}); //this line does same thing, different value //anchor.attachEvent("onmouseover",function(){alert(anchor)}); } //the idea is to set links to fire ordinal number, but all are fireing '3' </script>

    D 1 Reply Last reply
    0
    • H Hanzaplast

      hi guys. i have loop which needs to attach event on some elements. event must fire function and pass value from within loop. so here is problem... when loop is over all events fire same argument that is passed by ref not value at that time. <a href="#">link 1</a> <a href="#">link 2</a> <a href="#">link 3</a> <script type="text/javascript"> var anchorNiz = document.getElementsByTagName("a"); for(var i=0;i<anchorNiz.length;i++){ var anchor = anchorNiz[i]; anchor.attachEvent("onmouseover",function(){alert(i)}); //this line does same thing, different value //anchor.attachEvent("onmouseover",function(){alert(anchor)}); } //the idea is to set links to fire ordinal number, but all are fireing '3' </script>

      D Offline
      D Offline
      daveyerwin
      wrote on last edited by
      #2

      you will get 0, 1 and 2

      <a href="#">link 1</a>
      <a href="#">link 2</a>
      <a href="#">link 3</a>
      <script type="text/javascript">
      var anchorNiz = document.getElementsByTagName("a");
      for(var i=0;i<anchorNiz.length;i++){
      var anchor = anchorNiz[i];
      anchor.attachEvent("onmouseover",(function(){
      var arg = arguments[0];
      return function(){
      alert(arg);}})(i))
      }
      </script>

      also attachevent omly works in IE

      modified on Tuesday, September 14, 2010 2:24 PM

      H 1 Reply Last reply
      0
      • D daveyerwin

        you will get 0, 1 and 2

        <a href="#">link 1</a>
        <a href="#">link 2</a>
        <a href="#">link 3</a>
        <script type="text/javascript">
        var anchorNiz = document.getElementsByTagName("a");
        for(var i=0;i<anchorNiz.length;i++){
        var anchor = anchorNiz[i];
        anchor.attachEvent("onmouseover",(function(){
        var arg = arguments[0];
        return function(){
        alert(arg);}})(i))
        }
        </script>

        also attachevent omly works in IE

        modified on Tuesday, September 14, 2010 2:24 PM

        H Offline
        H Offline
        Hanzaplast
        wrote on last edited by
        #3

        thx. very interesting solution....

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups